Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

streamlit_speakup.py 2.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  1. import streamlit as st
  2. import weaviate
  3. from langchain_community.vectorstores import Weaviate
  4. from langchain_community.embeddings import HuggingFaceEmbeddings
  5. from langchain.chains import RetrievalQA
  6. from dotenv import load_dotenv
  7. import os
  8. from utils.hf_lazyclass import LazyHuggingFaceEndpoint
  9. from utils.tts_speech import text_to_speech_file
  10. load_dotenv()
  11. hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
  12. client = weaviate.Client(
  13. url="http://localhost:8081",
  14. )
  15. repo_id = "mistralai/Mistral-7B-Instruct-v0.2"
  16. llm = LazyHuggingFaceEndpoint(
  17. repo_id=repo_id, max_new_tokens=128, temperature=0.5, huggingfacehub_api_token = hf_token
  18. )
  19. model_name = "sentence-transformers/all-mpnet-base-v2"
  20. model_kwargs = {'device': 'cpu'}
  21. encode_kwargs = {'normalize_embeddings': False}
  22. hf = HuggingFaceEmbeddings(
  23. model_name=model_name,
  24. model_kwargs=model_kwargs,
  25. encode_kwargs=encode_kwargs
  26. )
  27. response = client.schema.get()
  28. weaviate_vectorstore = Weaviate(client=client, index_name=response['classes'][0]['class'], text_key="intro",by_text = False, embedding=hf)
  29. retriever = weaviate_vectorstore.as_retriever()
  30. qa_chain = RetrievalQA.from_chain_type(
  31. llm=llm, chain_type="stuff", retriever = retriever
  32. )
  33. st.title('Insurance Handbook QA with Voice')
  34. st.write("""
  35. This is a simple application where we utilize RAG and Text-to-Speech to answer all your queries regarding Insurance.
  36. In this application, we use the following tech stack:
  37. 1. Weaviate Vector Database with Docker Hosting
  38. 2. LangChain LLM Framework
  39. 3. HuggingFace Embedding Model all-mpnet-base-v2
  40. 4. HuggingFace Generative Model Mistral-7B-Instruct-v0.2
  41. 5. Elevenlabs Text-to-Speech Model
  42. 6. Streamlit for Front-End
  43. """)
  44. if 'prompt' not in st.session_state:
  45. st.session_state.prompt = ''
  46. if 'audiofile' not in st.session_state:
  47. st.session_state.audiofile = ''
  48. query = st.text_input("Ask Your Insurance Question👇", "")
  49. if st.button("Answer my Question"):
  50. st.session_state.prompt = query
  51. response = qa_chain.invoke(query)
  52. st.session_state.audiofile = text_to_speech_file(response['result'])
  53. st.audio(st.session_state.audiofile, format="audio/mpeg", loop = False)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...