Update app.py
Browse files
app.py
CHANGED
@@ -74,7 +74,7 @@ def stream_gemini_response(user_message: str, messages: list) -> Iterator[list]:
|
|
74 |
|
75 |
system_message = "μ¬μ©μ μ§λ¬Έμ λν΄ μμ½ν μ 보λ₯Ό μ 곡νλ μ λ¬Έ μ½ν μ΄μμ€ν΄νΈμ
λλ€."
|
76 |
system_prefix = """
|
77 |
-
λ°λμ νκΈλ‘ λ΅λ³νμμμ€. μΆλ ₯μ markdown νμμΌλ‘ μΆλ ₯νλΌ. λμ μ΄λ¦μ '
|
78 |
λΉμ μ 'μμ½ν μ§μ κ·Έλν(PharmKG) λ°μ΄ν° 100λ§ κ±΄ μ΄μμ νμ΅ν μ λ¬Έμ μΈ μμ½ν μ 보 AI μ‘°μΈμμ
λλ€.'
|
79 |
μ
λ ₯λ μ§λ¬Έμ λν΄ PharmKG λ°μ΄ν°μ
μμ κ°μ₯ κ΄λ ¨μ±μ΄ λμ μ 보λ₯Ό μ°Ύκ³ , μ΄λ₯Ό λ°νμΌλ‘ μμΈνκ³ μ²΄κ³μ μΈ λ΅λ³μ μ 곡ν©λλ€.
|
80 |
λ΅λ³μ λ€μ ꡬ쑰λ₯Ό λ°λ₯΄μμμ€:
|
@@ -206,94 +206,113 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="teal", secondary_hue="slate", n
|
|
206 |
</a>""")
|
207 |
|
208 |
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
|
225 |
-
clear_button = gr.Button("Clear Chat", scale=1)
|
226 |
-
|
227 |
-
# Add example prompts - removed file upload examples. Kept text focused examples.
|
228 |
-
example_prompts = [
|
229 |
-
["What is the generic name for Tylenol?"],
|
230 |
-
["What are the side effects of aspirin?"],
|
231 |
-
["Explain the mechanism of action of Metformin."],
|
232 |
-
["What are the uses of Warfarin?"],
|
233 |
-
["What is a typical dosage of amoxicillin?"]
|
234 |
-
]
|
235 |
-
|
236 |
-
gr.Examples(
|
237 |
-
examples=example_prompts,
|
238 |
-
inputs=input_box,
|
239 |
-
label="Examples: Try these prompts to see Gemini's thinking!",
|
240 |
-
examples_per_page=5 # Adjust as needed
|
241 |
-
)
|
242 |
-
|
243 |
-
|
244 |
-
# Set up event handlers
|
245 |
-
msg_store = gr.State("") # Store for preserving user message
|
246 |
-
|
247 |
-
input_box.submit(
|
248 |
-
lambda msg: (msg, msg, ""), # Store message and clear input
|
249 |
-
inputs=[input_box],
|
250 |
-
outputs=[msg_store, input_box, input_box],
|
251 |
-
queue=False
|
252 |
-
).then(
|
253 |
-
user_message, # Add user message to chat
|
254 |
-
inputs=[msg_store, chatbot],
|
255 |
-
outputs=[input_box, chatbot],
|
256 |
-
queue=False
|
257 |
-
).then(
|
258 |
-
stream_gemini_response, # Generate and stream response
|
259 |
-
inputs=[msg_store, chatbot],
|
260 |
-
outputs=chatbot
|
261 |
-
)
|
262 |
-
|
263 |
-
clear_button.click(
|
264 |
-
lambda: ([], "", ""),
|
265 |
-
outputs=[chatbot, input_box, msg_store],
|
266 |
-
queue=False
|
267 |
-
)
|
268 |
-
|
269 |
-
gr.Markdown( # Description moved to the bottom - updated for text-only
|
270 |
-
"""
|
271 |
-
<br><br><br> <!-- Add some vertical space -->
|
272 |
-
---
|
273 |
-
### About this Chatbot
|
274 |
-
This chatbot demonstrates the experimental 'thinking' capability of the **Gemini 2.0 Flash** model, now acting as a specialized pharmacology assistant.
|
275 |
-
You can observe the model's thought process as it generates responses, displayed with the "βοΈ Thinking" prefix.
|
276 |
-
|
277 |
-
**This chatbot is enhanced with a pharmacology dataset ("PharmKG") to provide more accurate and informed answers.**
|
278 |
-
|
279 |
-
**Try out the example prompts below to see Gemini in action!**
|
280 |
-
|
281 |
-
**Key Features:**
|
282 |
-
* Powered by Google's **Gemini 2.0 Flash** model.
|
283 |
-
* Shows the model's **thoughts** before the final answer (experimental feature).
|
284 |
-
* Supports **conversation history** for multi-turn chats.
|
285 |
-
* Uses **streaming** for a more interactive experience.
|
286 |
-
* Leverages a **pharmacology knowledge graph** to enhance responses.
|
287 |
-
**Instructions:**
|
288 |
-
1. Type your message in the input box below or select an example.
|
289 |
-
2. Press Enter or click Submit to send.
|
290 |
-
3. Observe the chatbot's "Thinking" process followed by the final response.
|
291 |
-
4. Use the "Clear Chat" button to start a new conversation.
|
292 |
-
|
293 |
-
*Please note*: The 'thinking' feature is experimental and the quality of thoughts may vary.
|
294 |
-
"""
|
295 |
-
)
|
296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
|
298 |
# Launch the interface
|
299 |
if __name__ == "__main__":
|
|
|
74 |
|
75 |
system_message = "μ¬μ©μ μ§λ¬Έμ λν΄ μμ½ν μ 보λ₯Ό μ 곡νλ μ λ¬Έ μ½ν μ΄μμ€ν΄νΈμ
λλ€."
|
76 |
system_prefix = """
|
77 |
+
λ°λμ νκΈλ‘ λ΅λ³νμμμ€. μΆλ ₯μ markdown νμμΌλ‘ μΆλ ₯νλΌ. λμ μ΄λ¦μ 'PharmAI'μ΄λ€.
|
78 |
λΉμ μ 'μμ½ν μ§μ κ·Έλν(PharmKG) λ°μ΄ν° 100λ§ κ±΄ μ΄μμ νμ΅ν μ λ¬Έμ μΈ μμ½ν μ 보 AI μ‘°μΈμμ
λλ€.'
|
79 |
μ
λ ₯λ μ§λ¬Έμ λν΄ PharmKG λ°μ΄ν°μ
μμ κ°μ₯ κ΄λ ¨μ±μ΄ λμ μ 보λ₯Ό μ°Ύκ³ , μ΄λ₯Ό λ°νμΌλ‘ μμΈνκ³ μ²΄κ³μ μΈ λ΅λ³μ μ 곡ν©λλ€.
|
80 |
λ΅λ³μ λ€μ ꡬ쑰λ₯Ό λ°λ₯΄μμμ€:
|
|
|
206 |
</a>""")
|
207 |
|
208 |
|
209 |
+
with gr.Tabs():
|
210 |
+
with gr.TabItem("Chat"):
|
211 |
+
chatbot = gr.Chatbot(
|
212 |
+
type="messages",
|
213 |
+
label="Gemini2.0 'Thinking' Chatbot (Streaming Output)", #Label now indicates streaming
|
214 |
+
render_markdown=True,
|
215 |
+
scale=1,
|
216 |
+
avatar_images=(None,"https://lh3.googleusercontent.com/oxz0sUBF0iYoN4VvhqWTmux-cxfD1rxuYkuFEfm1SFaseXEsjjE4Je_C_V3UQPuJ87sImQK3HfQ3RXiaRnQetjaZbjJJUkiPL5jFJ1WRl5FKJZYibUA=w214-h214-n-nu")
|
217 |
+
)
|
218 |
+
|
219 |
+
with gr.Row(equal_height=True):
|
220 |
+
input_box = gr.Textbox(
|
221 |
+
lines=1,
|
222 |
+
label="Chat Message",
|
223 |
+
placeholder="Type your message here...",
|
224 |
+
scale=4
|
225 |
+
)
|
226 |
+
|
227 |
+
clear_button = gr.Button("Clear Chat", scale=1)
|
228 |
+
|
229 |
+
# Add example prompts - removed file upload examples. Kept text focused examples.
|
230 |
+
example_prompts = [
|
231 |
+
["Explain the interplay between CYP450 enzymes and drug metabolism, specifically focusing on how enzyme induction or inhibition might affect the therapeutic efficacy of a drug such as warfarin."],
|
232 |
+
["Compare and contrast the mechanisms of action of ACE inhibitors and ARBs in managing hypertension, considering their effects on the renin-angiotensin-aldosterone system."],
|
233 |
+
["Describe the pathophysiology of type 2 diabetes and explain how metformin achieves its glucose-lowering effects, including any key considerations for patients with renal impairment."],
|
234 |
+
["Please discuss the mechanism of action and clinical significance of beta-blockers in the treatment of heart failure, with reference to specific beta-receptor subtypes and their effects on the cardiovascular system."],
|
235 |
+
["μμΈ νμ΄λ¨Έλ³μ λ³νμ리νμ κΈ°μ μ μ€λͺ
νκ³ , νμ¬ μ¬μ©λλ μ½λ¬Όλ€μ΄ μμ©νλ μ£Όμ νκ²μ μμΈν κΈ°μ νμμμ€. νΉν, μμΈνΈμ½λ¦°μμ€ν
λΌμ μ΅μ μ μ NMDA μμ©μ²΄ κΈΈνμ μ μμ© λ°©μκ³Ό μμμ μμλ₯Ό λΉκ΅ λΆμν΄ μ£Όμμμ€."],
|
236 |
+
["λ§μ± μ μ₯ μ§ν νμμμ λΉν μΉλ£λ₯Ό μν΄ μ¬μ©νλ μ리μ€λ‘ν¬μ΄μν΄ μ μ μ μ½λνμ λ° μ½λ ₯νμ νΉμ±μ μμΈν λΆμνκ³ , ν¬μ¬ μ©λ λ° ν¬μ¬ κ°κ²© κ²°μ μ μν₯μ λ―ΈμΉλ μμΈλ€μ μ€λͺ
ν΄ μ£Όμμμ€.",""],
|
237 |
+
["κ°κ²½λ³ νμμμ μ½λ¬Ό λμ¬μ λ³νλ₯Ό μ€λͺ
νκ³ , κ° κΈ°λ₯ μ νκ° μ½λ¬Ό ν¬μ¬λ μ‘°μ μ λ―ΈμΉλ μν₯μ ꡬ체μ μΈ μ½λ¬Ό μμμ ν¨κ» λ
Όμν΄ μ£Όμμμ€. νΉν, κ° λμ¬ ν¨μμ νμ± λ³νμ κ·Έ μμμ μ€μμ±μ μ€λͺ
ν΄ μ£Όμμμ€."]
|
238 |
+
]
|
239 |
+
|
240 |
+
gr.Examples(
|
241 |
+
examples=example_prompts,
|
242 |
+
inputs=input_box,
|
243 |
+
label="Examples: Try these prompts to see Gemini's thinking!",
|
244 |
+
examples_per_page=3 # Adjust as needed
|
245 |
+
)
|
246 |
+
|
247 |
+
|
248 |
+
# Set up event handlers
|
249 |
+
msg_store = gr.State("") # Store for preserving user message
|
250 |
+
|
251 |
+
input_box.submit(
|
252 |
+
lambda msg: (msg, msg, ""), # Store message and clear input
|
253 |
+
inputs=[input_box],
|
254 |
+
outputs=[msg_store, input_box, input_box],
|
255 |
+
queue=False
|
256 |
+
).then(
|
257 |
+
user_message, # Add user message to chat
|
258 |
+
inputs=[msg_store, chatbot],
|
259 |
+
outputs=[input_box, chatbot],
|
260 |
+
queue=False
|
261 |
+
).then(
|
262 |
+
stream_gemini_response, # Generate and stream response
|
263 |
+
inputs=[msg_store, chatbot],
|
264 |
+
outputs=chatbot
|
265 |
+
)
|
266 |
+
|
267 |
+
clear_button.click(
|
268 |
+
lambda: ([], "", ""),
|
269 |
+
outputs=[chatbot, input_box, msg_store],
|
270 |
+
queue=False
|
271 |
+
)
|
272 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
273 |
|
274 |
+
with gr.TabItem("Instructions"):
|
275 |
+
gr.Markdown(
|
276 |
+
"""
|
277 |
+
## PharmAI: Your Expert Pharmacology Assistant
|
278 |
+
|
279 |
+
Welcome to PharmAI, a specialized chatbot powered by Google's Gemini 2.0 Flash model. PharmAI is designed to provide expert-level information on pharmacology topics, leveraging a large dataset of pharmaceutical knowledge ("PharmKG").
|
280 |
+
|
281 |
+
**Key Features:**
|
282 |
+
|
283 |
+
* **Advanced Pharmacology Insights**: PharmAI provides responses that are structured, detailed, and based on a vast knowledge graph of pharmacology.
|
284 |
+
* **Inference and Reasoning**: The chatbot can handle complex, multi-faceted questions, showcasing its ability to reason and infer from available information.
|
285 |
+
* **Structured Responses**: Responses are organized logically to include definitions, mechanisms of action, indications, dosages, side effects, drug interactions, pharmacokinetics, and references when applicable.
|
286 |
+
* **Thinking Process Display**: You can observe the model's thought process as it generates a response (experimental feature).
|
287 |
+
* **Conversation History**: PharmAI remembers the previous parts of the conversation to provide more accurate and relevant information across multiple turns.
|
288 |
+
* **Streaming Output**: The chatbot streams responses for an interactive experience.
|
289 |
+
|
290 |
+
**How to Use PharmAI:**
|
291 |
+
|
292 |
+
1. **Start a Conversation**: Type your pharmacology question into the input box under the "Chat" tab. The chatbot is specifically designed to handle complex pharmacology inquiries.
|
293 |
+
|
294 |
+
2. **Use Example Prompts**: You can try out the example questions provided to see the model in action. These examples are formulated to challenge the chatbot to exhibit its expertise.
|
295 |
+
|
296 |
+
3. **Example Prompt Guidance**:
|
297 |
+
* **Mechanisms of Action**: Ask about how a specific drug works at the molecular level. Example: "Explain the mechanism of action of Metformin."
|
298 |
+
* **Drug Metabolism**: Inquire about how the body processes drugs. Example: "Explain the interplay between CYP450 enzymes and drug metabolism..."
|
299 |
+
* **Clinical Implications**: Pose questions about the clinical use of drugs in treating specific diseases. Example: "Discuss the mechanism of action and clinical significance of beta-blockers in heart failure..."
|
300 |
+
* **Pathophysiology and Drug Targets**: Ask about diseases, what causes them, and how drugs can treat them. Example: "Explain the pathophysiology of type 2 diabetes and how metformin works..."
|
301 |
+
* **Complex Multi-Drug Interactions**: Pose questions about how one drug can affect another drug in the body.
|
302 |
+
|
303 |
+
4. **Review Responses**: The chatbot will then present its response with a "Thinking" section that reveals its internal processing. Then it provides the more structured response, with sections including definition, mechanism of action, indications, etc.
|
304 |
+
|
305 |
+
5. **Clear Conversation**: Use the "Clear Chat" button to start a new session.
|
306 |
+
|
307 |
+
**Important Notes:**
|
308 |
+
|
309 |
+
* The 'thinking' feature is experimental, but it shows the steps the model took when creating the response.
|
310 |
+
* The quality of the response is highly dependent on the user prompt. Please be as descriptive as possible when asking questions to the best results.
|
311 |
+
* This model is focused specifically on pharmacology information, so questions outside this scope may not get relevant answers.
|
312 |
+
* This chatbot is intended as an informational resource and should not be used for medical diagnosis or treatment recommendations. Always consult with a healthcare professional for any medical advice.
|
313 |
+
|
314 |
+
"""
|
315 |
+
)
|
316 |
|
317 |
# Launch the interface
|
318 |
if __name__ == "__main__":
|