HydroX / app.py
wensjheng's picture
Create app.py
71a1638 verified
raw
history blame
3.17 kB
import gradio as gr
import requests
import json
GEMINI_API_KEY = "AIzaSyAZ3WSpx_o53zpmhIJRzR0JMsiBOTnttbg"
API_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={GEMINI_API_KEY}"
INTRO_TEXT = """
<div style='border:3px solid #2e8baf; border-radius:20px; padding:25px; background-color:#e6f3f9;'>
<h1 style='color:#1c5179; font-size:30px; text-align:center; background-color:white; border-radius:12px; padding:10px;'>氫鬆聊 - 專屬於您的健康諮詢夥伴</h1>
<p style='font-size:16px; color:#1c5179;'>本聊天機器人專為探討「氫分子 營養補充」搭配 <b>主流醫學</b> 、<b>臨床場景</b> 與 <b>健康保養</b> 的應用而設計。</p>
<p style='font-size:16px; color:#1c5179;'>我們致力於透過「氫分子 營養補充」,完成正規的臨床試驗、醫療期刊發表、查驗登記,來輔助主流醫學、幫助病人。</p>
</div>
"""
def call_gemini(message):
headers = {"Content-Type": "application/json"}
prompt = f"你是「氫分子 營養補充食品」諮詢機器人,專門協助分析網路上有佐證的的醫學資訊,要讓大家與你一起,享受生命奮鬥的美好。請根據含氫珊瑚鈣粉(Hydrogen-Rich Coral Calcium)健康膠囊的特性,提供關於以下情境的臨床支持建議。\n\n情境:{message}\n\n請使用自然語言醫學知識回答。必須基於實證不可以隨意編造 長話短說"
data = {
"contents": [
{
"parts": [{"text": prompt}]
}
]
}
response = requests.post(API_URL, headers=headers, data=json.dumps(data))
if response.status_code == 200:
try:
return response.json()['candidates'][0]['content']['parts'][0]['text']
except Exception as e:
return f"[錯誤解析回應]: {str(e)}"
else:
return f"[API 錯誤 {response.status_code}]: {response.text}"
with gr.Blocks(title="氫鬆聊 - HoHo Biotech Chatbot", css="""
body {
background-color: #d3eaf7 !important; /* 日本海淺藍色背景 */
}
.gradio-container {
background-color: #d3eaf7 !important;
}
.gr-button {
background-color: #1c5179 !important;
color: white !important;
font-weight: bold;
border-radius: 12px !important;
}
textarea, .gr-input, .gr-textbox {
background-color: #ffffff !important;
color: #1c5179 !important;
border: 1px solid #a6cfe2 !important;
}
.gr-chatbot {
background-color: #f0faff !important;
border-radius: 10px;
}
""") as demo:
gr.HTML(INTRO_TEXT)
chatbot = gr.Chatbot(height=400, label="對話區")
msg = gr.Textbox(label="請輸入您的問題", placeholder="例如:我正在接受__治療,可以吃___?")
with gr.Row():
ask = gr.Button("提問")
clear = gr.Button("清除對話")
def respond(message, history):
reply = call_gemini(message)
history.append((message, reply))
return "", history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
ask.click(respond, [msg, chatbot], [msg, chatbot])
clear.click(lambda: [], None, chatbot)
demo.launch()