|
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;'> |
|
<div style='background-color:#ffffff; border-radius:12px; padding:15px;'> |
|
<h1 style='color:#1c5179; font-size:30px; text-align:center; margin:0;'>氫鬆聊 - 專屬於您的健康諮詢夥伴</h1> |
|
</div> |
|
<p style='font-size:16px; color:#1c5179; margin-top:15px;'>本聊天機器人專為探討「氫分子 營養補充」搭配 <b style="color:#1c5179;">主流醫學</b> 、<b style="color:#1c5179;">臨床場景</b> 與 <b style="color:#1c5179;">健康保養</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="氫鬆聊 - 專屬於您的健康諮詢夥伴") 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() |