Kibo1 commited on
Commit
1474633
·
verified ·
1 Parent(s): 8b32b2d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+
3
+
4
+ df = pd.read_csv('./Mental_Health_FAQ.csv')
5
+
6
+ context_data = []
7
+ for i in range(len(df)):
8
+ context = ""
9
+ for j in range(3):
10
+ context += df.columns[j]
11
+ context += ": "
12
+ context += df.iloc[i][j]
13
+ context += " "
14
+ context_data.append(context)
15
+
16
+
17
+ import os
18
+
19
+ # Get the secret key from the environment
20
+ groq_key = os.environ.get('new_chatAPI_key')
21
+
22
+ ## LLM used for RAG
23
+ from langchain_groq import ChatGroq
24
+
25
+ llm = ChatGroq(model="llama-3.3-70b-versatile",api_key=groq_key)
26
+
27
+ ## Embedding model!
28
+ from langchain_huggingface import HuggingFaceEmbeddings
29
+ embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
30
+
31
+ # create vector store!
32
+ from langchain_chroma import Chroma
33
+
34
+ vectorstore = Chroma(
35
+ collection_name="medical_dataset_store",
36
+ embedding_function=embed_model,
37
+ )
38
+
39
+ # add data to vector nstore
40
+ vectorstore.add_texts(context_data)
41
+
42
+ retriever = vectorstore.as_retriever()
43
+
44
+ from langchain_core.prompts import PromptTemplate
45
+
46
+ template = ("""You are a mental health professional.
47
+ Use the provided context to answer the question.
48
+ If you don't know the answer, say so. Explain your answer in detail.
49
+ Do not discuss the context in your response; just provide the answer directly.
50
+
51
+ Context: {context}
52
+
53
+ Question: {question}
54
+
55
+ Answer:""")
56
+
57
+ rag_prompt = PromptTemplate.from_template(template)
58
+
59
+ from langchain_core.output_parsers import StrOutputParser
60
+ from langchain_core.runnables import RunnablePassthrough
61
+
62
+ rag_chain = (
63
+ {"context": retriever, "question": RunnablePassthrough()}
64
+ | rag_prompt
65
+ | llm
66
+ | StrOutputParser()
67
+ )
68
+
69
+ import gradio as gr
70
+
71
+ def rag_memory_stream(message, history):
72
+ partial_text = ""
73
+ for new_text in rag_chain.stream(message):
74
+ partial_text += new_text
75
+ yield partial_text
76
+
77
+ examples = [
78
+ "I am not in a good mood",
79
+ "what is the possible symptompts of depression?"
80
+ ]
81
+
82
+ description = "Real-time AI App with Groq API and LangChain to Answer medical questions"
83
+
84
+
85
+ title = "ThriveTalk Expert :) Try me!"
86
+ demo = gr.ChatInterface(fn=rag_memory_stream,
87
+ type="messages",
88
+ title=title,
89
+ description=description,
90
+ fill_height=True,
91
+ examples=examples,
92
+ theme="glass",
93
+ )
94
+
95
+
96
+ if __name__ == "__main__":
97
+ demo.launch()