Spaces:
Runtime error
Runtime error
Commit
·
642842b
1
Parent(s):
4676bbd
Update space
Browse files- app.py +83 -50
- requirements.txt +13 -1
app.py
CHANGED
@@ -1,63 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
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 |
-
|
27 |
|
28 |
-
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
-
demo = gr.ChatInterface(
|
46 |
-
respond,
|
47 |
-
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
|
|
|
|
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
from langchain_community.vectorstores import Chroma
|
3 |
+
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
4 |
+
from langchain.prompts import ChatPromptTemplate
|
5 |
+
from dotenv import load_dotenv
|
6 |
import gradio as gr
|
7 |
+
import openai
|
8 |
|
9 |
+
# Load environment variables from .env file
|
10 |
+
load_dotenv()
|
|
|
|
|
11 |
|
12 |
+
# Set OpenAI API key
|
13 |
+
openai.api_key = os.environ['OPENAI_API_KEY']
|
14 |
|
15 |
+
# Constants
|
16 |
+
CHROMA_PATH = "chroma"
|
17 |
+
PROMPT_TEMPLATE = """
|
18 |
+
Answer the question based only on the following context:
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
{context}
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
---
|
23 |
|
24 |
+
Answer the question based on the above context: {question}
|
25 |
+
"""
|
26 |
|
27 |
+
# Function to process user input and generate response
|
28 |
+
def generate_response(query_text, history):
|
29 |
+
# Prepare the DB
|
30 |
+
embedding_function = OpenAIEmbeddings()
|
31 |
+
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
|
|
|
|
|
|
|
32 |
|
33 |
+
# Search the DB
|
34 |
+
results = db.similarity_search_with_relevance_scores(query_text, k=3)
|
35 |
+
if len(results) == 0 or results[0][1] < 0.7:
|
36 |
+
response_text = "🤔 Unable to find matching results."
|
37 |
+
else:
|
38 |
+
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
|
39 |
+
prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
|
40 |
+
prompt = prompt_template.format(context=context_text, question=query_text)
|
41 |
+
|
42 |
+
# Generate response
|
43 |
+
model = ChatOpenAI(model="gpt-4o")
|
44 |
+
response_text = model.invoke(prompt).content
|
45 |
+
# sources = [doc.metadata.get("source", None) for doc, _score in results]
|
46 |
+
# response_text += f"\n\n**Sources:** {', '.join(sources)}"
|
47 |
+
|
48 |
+
history.append(("You 🗣️", query_text))
|
49 |
+
history.append(("Biomedical Informatics Assistant 🤖", response_text))
|
50 |
+
return history, ""
|
51 |
+
|
52 |
+
# Gradio Interface
|
53 |
+
with gr.Blocks() as demo:
|
54 |
+
gr.Markdown("<h1 style='text-align: center; color: white;'>AI-Powered Chat Interface for Biomedical Informatics 🤖</h1>")
|
55 |
+
|
56 |
+
chatbot = gr.Chatbot(elem_id="chatbot")
|
57 |
+
|
58 |
+
with gr.Row():
|
59 |
+
with gr.Column(scale=7):
|
60 |
+
query_text = gr.Textbox(
|
61 |
+
show_label=False,
|
62 |
+
placeholder="Type your question here ✍️...",
|
63 |
+
lines=1,
|
64 |
+
elem_id="input_box"
|
65 |
+
)
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# Set up interactions
|
69 |
+
query_text.submit(generate_response, [query_text, chatbot], [chatbot, query_text])
|
70 |
|
71 |
+
# Custom CSS
|
72 |
+
demo.css = """
|
73 |
+
#input_box {
|
74 |
+
font-size: 18px;
|
75 |
+
padding: 10px;
|
76 |
+
}
|
77 |
+
#chatbot .message {
|
78 |
+
font-size: 18px;
|
79 |
+
}
|
80 |
+
#chatbot .user {
|
81 |
+
background-color: #333;
|
82 |
+
color: white;
|
83 |
+
font-size: 32px;
|
84 |
+
}
|
85 |
+
#chatbot .assistant {
|
86 |
+
background-color: #007BFF;
|
87 |
+
color: white;
|
88 |
+
font-size: 32px;
|
89 |
+
}
|
90 |
+
body {
|
91 |
+
background-color: #ffffff;
|
92 |
+
}
|
93 |
+
"""
|
94 |
+
|
95 |
if __name__ == "__main__":
|
96 |
+
demo.launch(share=True)
|
requirements.txt
CHANGED
@@ -1 +1,13 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
python-dotenv==1.0.1 # For reading environment variables stored in .env file
|
2 |
+
langchain==0.2.2
|
3 |
+
langchain-community==0.2.3
|
4 |
+
langchain-openai==0.1.8 # For embeddings
|
5 |
+
unstructured==0.14.4 # Document loading
|
6 |
+
# onnxruntime==1.17.1 # chromadb dependency: on Mac use `conda install onnxruntime -c conda-forge`
|
7 |
+
# For Windows users, install Microsoft Visual C++ Build Tools first
|
8 |
+
# install onnxruntime before installing `chromadb`
|
9 |
+
chromadb==0.5.0 # Vector storage
|
10 |
+
openai==1.31.1 # For embeddings
|
11 |
+
tiktoken==0.7.0 # For embeddings
|
12 |
+
|
13 |
+
# install markdown depenendies with: `pip install "unstructured[md]"` after install the requirements file. Leave this line commented out.
|