wensjheng commited on
Commit
71a1638
·
verified ·
1 Parent(s): 85fe325

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+
5
+ GEMINI_API_KEY = "AIzaSyAZ3WSpx_o53zpmhIJRzR0JMsiBOTnttbg"
6
+ API_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={GEMINI_API_KEY}"
7
+
8
+ INTRO_TEXT = """
9
+ <div style='border:3px solid #2e8baf; border-radius:20px; padding:25px; background-color:#e6f3f9;'>
10
+ <h1 style='color:#1c5179; font-size:30px; text-align:center; background-color:white; border-radius:12px; padding:10px;'>氫鬆聊 - 專屬於您的健康諮詢夥伴</h1>
11
+ <p style='font-size:16px; color:#1c5179;'>本聊天機器人專為探討「氫分子 營養補充」搭配 <b>主流醫學</b> 、<b>臨床場景</b> 與 <b>健康保養</b> 的應用而設計。</p>
12
+ <p style='font-size:16px; color:#1c5179;'>我們致力於透過「氫分子 營養補充」,完成正規的臨床試驗、醫療期刊發表、查驗登記,來輔助主流醫學、幫助病人。</p>
13
+ </div>
14
+ """
15
+
16
+
17
+ def call_gemini(message):
18
+ headers = {"Content-Type": "application/json"}
19
+ prompt = f"你是「氫分子 營養補充食品」諮詢機器人,專門協助分析網路上有佐證的的醫學資訊,要讓大家與你一起,享受生命奮鬥的美好。請根據含氫珊瑚鈣粉(Hydrogen-Rich Coral Calcium)健康膠囊的特性,提供關於以下情境的臨床支持建議。\n\n情境:{message}\n\n請使用自然語言醫學知識回答。必須基於實證不可以隨意編造 長話短說"
20
+ data = {
21
+ "contents": [
22
+ {
23
+ "parts": [{"text": prompt}]
24
+ }
25
+ ]
26
+ }
27
+ response = requests.post(API_URL, headers=headers, data=json.dumps(data))
28
+ if response.status_code == 200:
29
+ try:
30
+ return response.json()['candidates'][0]['content']['parts'][0]['text']
31
+ except Exception as e:
32
+ return f"[錯誤解析回應]: {str(e)}"
33
+ else:
34
+ return f"[API 錯誤 {response.status_code}]: {response.text}"
35
+
36
+ with gr.Blocks(title="氫鬆聊 - HoHo Biotech Chatbot", css="""
37
+ body {
38
+ background-color: #d3eaf7 !important; /* 日本海淺藍色背景 */
39
+ }
40
+ .gradio-container {
41
+ background-color: #d3eaf7 !important;
42
+ }
43
+ .gr-button {
44
+ background-color: #1c5179 !important;
45
+ color: white !important;
46
+ font-weight: bold;
47
+ border-radius: 12px !important;
48
+ }
49
+ textarea, .gr-input, .gr-textbox {
50
+ background-color: #ffffff !important;
51
+ color: #1c5179 !important;
52
+ border: 1px solid #a6cfe2 !important;
53
+ }
54
+ .gr-chatbot {
55
+ background-color: #f0faff !important;
56
+ border-radius: 10px;
57
+ }
58
+ """) as demo:
59
+ gr.HTML(INTRO_TEXT)
60
+ chatbot = gr.Chatbot(height=400, label="對話區")
61
+ msg = gr.Textbox(label="請輸入您的問題", placeholder="例如:我正在接受__治療,可以吃___?")
62
+ with gr.Row():
63
+ ask = gr.Button("提問")
64
+ clear = gr.Button("清除對話")
65
+
66
+ def respond(message, history):
67
+ reply = call_gemini(message)
68
+ history.append((message, reply))
69
+ return "", history
70
+
71
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
72
+ ask.click(respond, [msg, chatbot], [msg, chatbot])
73
+ clear.click(lambda: [], None, chatbot)
74
+
75
+ demo.launch()