repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import queue |
| #2 | |
| #3 | import streamlit as st |
| #4 | |
| #5 | from embedchain import App |
| #6 | from embedchain.config import BaseLlmConfig |
| #7 | from embedchain.helpers.callbacks import StreamingStdOutCallbackHandlerYield, generate |
| #8 | |
| #9 | |
| #10 | @st.cache_resource |
| #11 | def unacademy_ai(): |
| #12 | app = App() |
| #13 | return app |
| #14 | |
| #15 | |
| #16 | app = unacademy_ai() |
| #17 | |
| #18 | assistant_avatar_url = "https://cdn-images-1.medium.com/v2/resize:fit:1200/1*LdFNhpOe7uIn-bHK9VUinA.jpeg" |
| #19 | |
| #20 | st.markdown(f"# <img src='{assistant_avatar_url}' width={35} /> Unacademy UPSC AI", unsafe_allow_html=True) |
| #21 | |
| #22 | styled_caption = """ |
| #23 | <p style="font-size: 17px; color: #aaa;"> |
| #24 | 🚀 An <a href="https://github.com/embedchain/embedchain">Embedchain</a> app powered with Unacademy\'s UPSC data! |
| #25 | </p> |
| #26 | """ |
| #27 | st.markdown(styled_caption, unsafe_allow_html=True) |
| #28 | |
| #29 | with st.expander(":grey[Want to create your own Unacademy UPSC AI?]"): |
| #30 | st.write( |
| #31 | """ |
| #32 | ```bash |
| #33 | pip install embedchain |
| #34 | ``` |
| #35 | |
| #36 | ```python |
| #37 | from embedchain import App |
| #38 | unacademy_ai_app = App() |
| #39 | unacademy_ai_app.add( |
| #40 | "https://unacademy.com/content/upsc/study-material/plan-policy/atma-nirbhar-bharat-3-0/", |
| #41 | data_type="web_page" |
| #42 | ) |
| #43 | unacademy_ai_app.chat("What is Atma Nirbhar 3.0?") |
| #44 | ``` |
| #45 | |
| #46 | For more information, checkout the [Embedchain docs](https://docs.embedchain.ai/get-started/quickstart). |
| #47 | """ |
| #48 | ) |
| #49 | |
| #50 | if "messages" not in st.session_state: |
| #51 | st.session_state.messages = [ |
| #52 | { |
| #53 | "role": "assistant", |
| #54 | "content": """Hi, I'm Unacademy UPSC AI bot, who can answer any questions related to UPSC preparation. |
| #55 | Let me help you prepare better for UPSC.\n |
| #56 | Sample questions: |
| #57 | - What are the subjects in UPSC CSE? |
| #58 | - What is the CSE scholarship price amount? |
| #59 | - What are different indian calendar forms? |
| #60 | """, |
| #61 | } |
| #62 | ] |
| #63 | |
| #64 | for message in st.session_state.messages: |
| #65 | role = message["role"] |
| #66 | with st.chat_message(role, avatar=assistant_avatar_url if role == "assistant" else None): |
| #67 | st.markdown(message["content"]) |
| #68 | |
| #69 | if prompt := st.chat_input("Ask me anything!"): |
| #70 | with st.chat_message("user"): |
| #71 | st.markdown(prompt) |
| #72 | st.session_state.messages.append({"role": "user", "content": prompt}) |
| #73 | |
| #74 | with st.chat_message("assistant", avatar=assistant_avatar_url): |
| #75 | msg_placeholder = st.empty() |
| #76 | msg_placeholder.markdown("Thinking...") |
| #77 | full_response = "" |
| #78 | |
| #79 | q = queue.Queue() |
| #80 | |
| #81 | def app_response(result): |
| #82 | llm_config = app.llm.config.as_dict() |
| #83 | llm_config["callbacks"] = [StreamingStdOutCallbackHandlerYield(q=q)] |
| #84 | config = BaseLlmConfig(**llm_config) |
| #85 | answer, citations = app.chat(prompt, config=config, citations=True) |
| #86 | result["answer"] = answer |
| #87 | result["citations"] = citations |
| #88 | |
| #89 | results = {} |
| #90 | |
| #91 | for answer_chunk in generate(q): |
| #92 | full_response += answer_chunk |
| #93 | msg_placeholder.markdown(full_response) |
| #94 | |
| #95 | answer, citations = results["answer"], results["citations"] |
| #96 | |
| #97 | if citations: |
| #98 | full_response += "\n\n**Sources**:\n" |
| #99 | sources = list(set(map(lambda x: x[1], citations))) |
| #100 | for i, source in enumerate(sources): |
| #101 | full_response += f"{i+1}. {source}\n" |
| #102 | |
| #103 | msg_placeholder.markdown(full_response) |
| #104 | st.session_state.messages.append({"role": "assistant", "content": full_response}) |
| #105 |