dntwaritag commited on
Commit
55e745a
·
verified ·
1 Parent(s): 5b19033

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -58
app.py CHANGED
@@ -1,64 +1,83 @@
 
 
 
 
 
 
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
 
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
+ # # Import embedding model
2
+ from langchain_huggingface import HuggingFaceEmbeddings
3
+ embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
4
+ from langchain_core.prompts import PromptTemplate
5
+ from langchain_core.output_parsers import StrOutputParser
6
+ from langchain_core.runnables import RunnablePassthrough
7
  import gradio as gr
8
+ import pandas as pd
9
+
10
+ from langchain_groq import ChatGroq
11
+ # Create a vector store...
12
+ from langchain_chroma import Chroma
13
+ import os
14
+
15
+ vectorstore = Chroma(
16
+ collection_name="medical_dataset_store",
17
+ embedding_function=embed_model,
18
+ persist_directory="./",
19
+ )
20
+ vectorstore.get().keys()
21
+
22
+ # Load the dataset to be used.
23
+ context = pd.read_csv("./drugs_side_effects_drugs_com.csv")
24
+
25
+ # Because the vector store is empty... Add your context data.
26
+ vectorstore.add_texts(context)
27
+
28
+ retriever = vectorstore.as_retriever()
29
+
30
+
31
+ template = ("""
32
+ You are a medical expert specializing in pharmacology.
33
+ Your task is to use the provided context to answer questions about drug side effects for patients.
34
+ Please follow these guidelines:
35
+ - Provide accurate and detailed answers based on the context.
36
+ - If you don't know the answer, clearly state that you don't know.
37
+ - Do not reference the context directly in your response; just provide the answer.
38
+ - Ensure your answers are clear, concise, and informative.
39
+ Context: {context}
40
+ Question: {question}
41
+ Answer:
42
+ """)
43
+ rag_prompt = PromptTemplate.from_template(template)
44
+
45
+ # Initialize the model
46
+ llm_model = ChatGroq(model="llama-3.3-70b-versatile", api_key=os.environ.get("GROQ_API_KEY"))
47
+ rag_chain = (
48
+ {"context": retriever, "question": RunnablePassthrough()}
49
+ | rag_prompt
50
+ | llm_model
51
+ | StrOutputParser()
52
+ )
53
+
54
+ def rag_memory_stream(message, history):
55
+ partial_text = ""
56
+ for new_text in rag_chain.stream(message):
57
+ partial_text += new_text
58
+ yield partial_text
59
+
60
+ examples = [
61
+ "What is a drug ?",
62
+ "What are the side effects of lisinopril?"
63
+ ]
64
+
65
+ description = "Real-Time AI-Powered Medical Assistant: Drug Side Effect Queries Chatbot"
66
+
67
+
68
+ title = "AI-Powered Medical Chatbot :) Try me!"
69
+ demo = gr.ChatInterface(fn=rag_memory_stream,
70
+ type="messages",
71
+ title=title,
72
+ description=description,
73
+ fill_height=True,
74
+ examples=examples,
75
+ theme="glass",
76
  )
77
 
78
+ # Launch the application and make it sharable
79
+ demo.launch(share=True)
80
+
81
 
82
  if __name__ == "__main__":
83
  demo.launch()