ginipick commited on
Commit
048d8b8
Β·
verified Β·
1 Parent(s): 12ac381

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -87
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 ν˜•μ‹μœΌλ‘œ 좜λ ₯ν•˜λΌ. λ„ˆμ˜ 이름은 'μΆ”λ‘  기반 PharmAI'이닀.
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
- chatbot = gr.Chatbot(
210
- type="messages",
211
- label="Gemini2.0 'Thinking' Chatbot (Streaming Output)", #Label now indicates streaming
212
- render_markdown=True,
213
- scale=1,
214
- avatar_images=(None,"https://lh3.googleusercontent.com/oxz0sUBF0iYoN4VvhqWTmux-cxfD1rxuYkuFEfm1SFaseXEsjjE4Je_C_V3UQPuJ87sImQK3HfQ3RXiaRnQetjaZbjJJUkiPL5jFJ1WRl5FKJZYibUA=w214-h214-n-nu")
215
- )
216
-
217
- with gr.Row(equal_height=True):
218
- input_box = gr.Textbox(
219
- lines=1,
220
- label="Chat Message",
221
- placeholder="Type your message here...",
222
- scale=4
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__":