munawsa's picture
Update app.py
6358a38 verified
import gradio as gr
import requests
import os
API_KEY = os.getenv("OPENROUTER_API_KEY")
def drugexplainer(input_obat, selected_model):
try:
prompt = f"""
Kamu adalah seorang apoteker jenius yang sangat ahli dalam edukasi kesehatan
Tugasmu adalah menjelaskan cara kerja obat yang ditanyakan oleh pengguna yang merupakan masyarakat umum
Patuhi aturan ketat berikut dalam menyusun jawaban:
1. JANGAN PERNAH menggunakan istilah medis yang rumit
2. Gunakan gaya bahasa yang sederhana
Sekarang, jelaskan obat berikut: {input_obat}
"""
response = requests.post(
url="https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json={
"model": selected_model,
"messages": [
{"role": "user", "content": prompt}
]
}
)
data = response.json()
# Check if 'choices' key exists and is not empty
if "choices" in data and len(data["choices"]) > 0:
return data["choices"][0]["message"]["content"]
else:
# Handle cases where 'choices' might be missing or empty
return f"Error: No content received from the model. Response: {data}"
except Exception as e:
return f"Error: {str(e)}"
interface = gr.Interface(
fn=drugexplainer,
inputs=[
gr.Textbox(label="Nama Obat", placeholder="Masukkan nama obat, misal: Omeprazole, Captopril, Metformin..."),
gr.Dropdown(
label="Pilih Model AI",
choices=["baidu/cobuddy:free", "openrouter/owl-alpha", "nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:free", "arcee-ai/trinity-large-thinking:free", "minimax/minimax-m2.5:free", "liquid/lfm-2.5-1.2b-thinking:free", "qwen/qwen3-next-80b-a3b-instruct:free"],
value="nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:free"
)
],
outputs=gr.Textbox(label="Penjelasan Apoteker"),
title="πŸ’Š Drug Mechanism Explainer πŸ’Š",
description="""
<div style='font-family: sans-serif; line-height: 1.5;'>
<p><strong>Selamat datang di Drug Mechanism Explainer!</strong> 🌟</p>
<p>Aplikasi ini dirancang khusus untuk menjelaskan mekanisme kerja obat yang rumit menjadi sederhana yang mudah dipahami oleh masyarakat awam.</p>
<p>Dibuat dalam rangka penyelesaian Project IBM SkillsBuild University Education oleh Munawsa dari Universitas Indonesia.</p>
<hr style='border: 0; border-top: 1px solid #444; margin: 10px 0;'>
<p style='font-size: 0.9em; color: #aaa;'>
πŸ’‘ <strong>Cara Pakai:</strong> Ketik nama obat (generik/merek) ➑️ Pilih model AI cadangan jika respons melambat ➑️ Klik <strong>Submit</strong>.
</p>
</div>
"""
)
interface.launch()