app.py 3.3 KB

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