Spaces:
Sleeping
Sleeping
Boghdady9
commited on
Commit
·
4333355
1
Parent(s):
9b94b5d
Add application file
Browse files- app.py +131 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from typing import List, Tuple
|
4 |
+
|
5 |
+
# Initialize the model
|
6 |
+
generator = pipeline("text-generation", model="BoghdadyJR/chatbot-dialogue-training-1734727108")
|
7 |
+
|
8 |
+
# Shortened system prompt
|
9 |
+
system_prompt="""
|
10 |
+
You are an AI medical information assistant designed to provide general health information and guidance. Important disclaimers:
|
11 |
+
|
12 |
+
1. You are NOT a substitute for professional medical care. You cannot diagnose conditions, prescribe medications, or provide personalized medical advice.
|
13 |
+
|
14 |
+
2. Always advise users to consult qualified healthcare professionals for:
|
15 |
+
- Specific medical diagnoses
|
16 |
+
- Treatment decisions
|
17 |
+
- Changes to existing medications or treatments
|
18 |
+
- Medical emergencies
|
19 |
+
- Mental health crises
|
20 |
+
|
21 |
+
Your primary functions are to:
|
22 |
+
- Provide general, evidence-based health information from reliable medical sources
|
23 |
+
- Explain common medical terms and procedures in simple language
|
24 |
+
- Offer general wellness and preventive health information
|
25 |
+
- Help users understand basic medical concepts
|
26 |
+
- Guide users on when to seek professional medical care
|
27 |
+
- Share publicly available information about common conditions, symptoms, and general treatment approaches
|
28 |
+
|
29 |
+
When responding:
|
30 |
+
- Be clear, compassionate, and professional
|
31 |
+
- Use plain language that is easy to understand
|
32 |
+
- Include relevant disclaimers when appropriate
|
33 |
+
- Cite reputable medical sources when possible
|
34 |
+
- Maintain user privacy and confidentiality
|
35 |
+
- Express empathy while remaining objective
|
36 |
+
- Clearly state limitations and direct to professional care when needed
|
37 |
+
|
38 |
+
If users describe emergency situations or severe symptoms, immediately direct them to seek emergency medical care or call their local emergency services.
|
39 |
+
|
40 |
+
Remember: Your role is to inform and educate, not to diagnose or treat. When in doubt, always encourage users to consult with qualified healthcare professionals.
|
41 |
+
"""
|
42 |
+
|
43 |
+
|
44 |
+
class ChatMemory:
|
45 |
+
def __init__(self, max_history: int = 5):
|
46 |
+
self.max_history = max_history
|
47 |
+
self.conversation_history: List[Tuple[str, str]] = []
|
48 |
+
|
49 |
+
def add_interaction(self, user_message: str, bot_response: str):
|
50 |
+
self.conversation_history.append((user_message, bot_response))
|
51 |
+
if len(self.conversation_history) > self.max_history:
|
52 |
+
self.conversation_history = self.conversation_history[-self.max_history:]
|
53 |
+
|
54 |
+
def get_context(self) -> str:
|
55 |
+
return "\n".join([
|
56 |
+
f"User: {interaction[0]}\nAssistant: {interaction[1]}"
|
57 |
+
for interaction in self.conversation_history
|
58 |
+
])
|
59 |
+
|
60 |
+
|
61 |
+
class Chatbot:
|
62 |
+
def __init__(self):
|
63 |
+
self.memory = ChatMemory()
|
64 |
+
|
65 |
+
def generate_response(self, message: str) -> str:
|
66 |
+
messages = [
|
67 |
+
{"role": "system", "content": system_prompt},
|
68 |
+
{"role": "user", "content": message}
|
69 |
+
]
|
70 |
+
|
71 |
+
try:
|
72 |
+
response = generator(messages, max_new_tokens=512, return_full_text=False)[0]
|
73 |
+
generated_text = response["generated_text"]
|
74 |
+
|
75 |
+
self.memory.add_interaction(message, generated_text)
|
76 |
+
|
77 |
+
return generated_text
|
78 |
+
except Exception as e:
|
79 |
+
return f"I apologize, but I encountered an error: {str(e)}"
|
80 |
+
|
81 |
+
|
82 |
+
# Initialize the chatbot
|
83 |
+
chatbot = Chatbot()
|
84 |
+
|
85 |
+
# Create the Gradio interface
|
86 |
+
with gr.Blocks() as demo:
|
87 |
+
gr.Markdown("# Medical Information Assistant")
|
88 |
+
|
89 |
+
chat_history = gr.Chatbot(
|
90 |
+
value=[],
|
91 |
+
elem_id="chatbot",
|
92 |
+
bubble_full_width=False,
|
93 |
+
)
|
94 |
+
|
95 |
+
with gr.Row():
|
96 |
+
msg = gr.Textbox(
|
97 |
+
show_label=False,
|
98 |
+
placeholder="Type your health-related question here...",
|
99 |
+
)
|
100 |
+
submit_button = gr.Button("➤", scale=0.1)
|
101 |
+
|
102 |
+
|
103 |
+
def user(user_message: str, history: list) -> tuple:
|
104 |
+
if not user_message.strip():
|
105 |
+
return "", history
|
106 |
+
history = history + [[user_message, None]]
|
107 |
+
return "", history
|
108 |
+
|
109 |
+
|
110 |
+
def bot(history: list) -> list:
|
111 |
+
if not history:
|
112 |
+
return history
|
113 |
+
|
114 |
+
user_message = history[-1][0]
|
115 |
+
bot_message = chatbot.generate_response(user_message)
|
116 |
+
history[-1][1] = bot_message
|
117 |
+
|
118 |
+
return history
|
119 |
+
|
120 |
+
|
121 |
+
msg.submit(user, [msg, chat_history], [msg, chat_history], queue=False).then(
|
122 |
+
bot, chat_history, chat_history
|
123 |
+
)
|
124 |
+
|
125 |
+
submit_button.click(user, [msg, chat_history], [msg, chat_history], queue=False).then(
|
126 |
+
bot, chat_history, chat_history
|
127 |
+
)
|
128 |
+
|
129 |
+
# Launch the interface
|
130 |
+
if __name__ == "__main__":
|
131 |
+
demo.launch()
|
requirements.txt
ADDED
File without changes
|