HydroX / app.py
wensjheng's picture
Update app.py
e18810c verified
raw
history blame
3.78 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>
<h1>氫鬆聊 - 專屬於您的健康諮詢夥伴 HydroChat - Your Personalized Assistant about Molecular Hydrogen </h1>
<p>本聊天機器人專為探討「氫分子 營養補充」搭配 <b>主流醫學</b> 、<b>臨床場景</b> 與 <b>健康保養</b> 的應用而設計。</p>
<p>我們致力於透過氫分子 營養補充,完成正規的臨床試驗、醫療期刊發表、查驗登記,來輔助主流醫學、幫助病人。</p>
</div>
"""
def call_gemini(message):
headers = {"Content-Type": "application/json"}
lang = detect(message) # 自動偵測語言
if lang == "zh-cn" or lang == "zh-tw":
prompt = f"""你是「氫分子 營養補充食品」的諮詢機器人,專門協助使用者了解此類產品在醫學上的潛在應用。
請根據以下指導原則提供建議與回應:
1. 鼓勵使用者思考如何將含氫分子食品與現有保健品或處方藥**搭配使用**,以期達成**協同作用**,但同時需提醒使用者就個別用藥與醫療人員確認。
2. 在衛教或回答產品效果問題時,請**聚焦於氫分子(Molecular Hydrogen)的研究證據**與已知機制,包括抗氧化、抗發炎、細胞保護等面向。
3. 回答必須**簡明、實證、不可編造**,避免誇大療效。
情境:{message}"""
elif lang == "en":
prompt = f"""You are a scientific assistant chatbot specialized in hydrogen-rich coral calcium supplements. Please provide clinical guidance based on the following context (evidence-based, brief, no hallucinations):
Context: {message}"""
else:
# fallback for other languages, keep message in detected language
prompt = f"""You are a chatbot specializing in molecular hydrogen supplements. Your purpose is to help users explore its potential health benefits based on scientific evidence.
Please follow these behavioral principles:
1. Encourage users to consider **synergistic use** of molecular hydrogen with their existing supplements or medications, while reminding them to consult with medical professionals.
2. When providing health education or answering about efficacy, focus specifically on **Molecular Hydrogen** and its **documented effects** such as antioxidation, anti-inflammation, and cellular protection.
3. Be **concise, evidence-based, and avoid speculative or exaggerated claims**.
Context: {message}"""
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="氫鬆聊 - 專屬於您的健康諮詢夥伴") as demo:
gr.HTML(INTRO_TEXT)
chatbot = gr.Chatbot(height=400)
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()