blaa1997 commited on
Commit
83ba0e2
·
verified ·
1 Parent(s): 8f7ce29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -58
app.py CHANGED
@@ -1,62 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
  )
61
 
62
 
 
1
+ import pandas as pd
2
+
3
+
4
+ df = pd.read_csv('./medical_data.csv')
5
+ df1=pd.read_csv('./DrugData.csv')
6
+ df11=pd.read_csv('./drugs_side_effects_drugs_com.csv')
7
+
8
+ context_data = []
9
+ for i in range(len(df)):
10
+ context = ""
11
+ for j in range(3):
12
+ context += df.columns[j]
13
+ context += ": "
14
+ context += df.iloc[i][j]
15
+ context += " "
16
+ context_data.append(context)
17
+
18
+ context_data # Initialize the list to store context data
19
+
20
+ for i in range(len(df1)): # Iterate through the rows of df1
21
+ context = ""
22
+ for j in range(19): # Iterate through the first 19 columns
23
+ context += df1.columns[j] # Add the column name
24
+ context += ": "
25
+ context += str(df1.iloc[i][j]) # Convert the value to a string
26
+ context += " " # Add a space between entries
27
+ context_data.append(context) # Append the generated context to the list
28
+ context_data # Initialize the list to store context data
29
+
30
+ for i in range(len(df11)): # Iterate through the rows of df1
31
+ context = ""
32
+ for j in range(17): # Iterate through the first 19 columns
33
+ context += df11.columns[j] # Add the column name
34
+ context += ": "
35
+ context += str(df11.iloc[i][j]) # Convert the value to a string
36
+ context += " " # Add a space between entries
37
+ context_data.append(context) # Append the generated context to the list
38
+
39
+
40
+ import os
41
+
42
+ # Get the secret key from the environment
43
+ groq_key = os.environ.get('groq_key')
44
+
45
+ ## LLM used for RAG
46
+ from langchain_groq import ChatGroq
47
+
48
+ llm = ChatGroq(model="llama-3.1-70b-versatile",api_key=groq_key)
49
+
50
+ ## Embedding model!
51
+ from langchain_huggingface import HuggingFaceEmbeddings
52
+ embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
53
+
54
+ # create vector store!
55
+ from langchain_chroma import Chroma
56
+
57
+ vectorstore = Chroma(
58
+ collection_name="medical_dataset_store",
59
+ embedding_function=embed_model,
60
+ persist_directory="./",
61
+ )
62
+
63
+ # add data to vector nstore
64
+ vectorstore.add_texts(context_data)
65
+
66
+ retriever = vectorstore.as_retriever()
67
+
68
+ from langchain_core.prompts import PromptTemplate
69
+
70
+ template = ("""You are a medical expert.
71
+ Use the provided context to answer the question.
72
+ If you don't know the answer, say so. Explain your answer in detail.
73
+ Do not discuss the context in your response; just provide the answer directly.
74
+ Context: {context}
75
+ Question: {question}
76
+ Answer:""")
77
+
78
+ rag_prompt = PromptTemplate.from_template(template)
79
+
80
+ from langchain_core.output_parsers import StrOutputParser
81
+ from langchain_core.runnables import RunnablePassthrough
82
+
83
+ rag_chain = (
84
+ {"context": retriever, "question": RunnablePassthrough()}
85
+ | rag_prompt
86
+ | llm
87
+ | StrOutputParser()
88
+ )
89
+
90
  import gradio as gr
91
+
92
+ def rag_memory_stream(text):
93
+ partial_text = ""
94
+ for new_text in rag_chain.stream(text):
95
+ partial_text += new_text
96
+ yield partial_text
97
+
98
+ examples = ['I feel dizzy', 'what is the possible sickness for fatigue']
99
+
100
+
101
+
102
+
103
+ title = "Real-time AI App with Groq API and LangChain to Answer medical questions"
104
+ demo = gr.Interface(
105
+ title=title,
106
+ fn=rag_memory_stream,
107
+ inputs="text",
108
+ outputs="text",
109
+ examples=examples,
110
+ allow_flagging="never",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  )
112
 
113