wuki8888 commited on
Commit
e491cd7
·
verified ·
1 Parent(s): c6a30f9

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +81 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import gradio as gr # 確認已正確導入 gradio
3
+ import os
4
+ import requests
5
+ import json
6
+
7
+ # 從 Hugging Face secrets 中讀取 OpenAI API 金鑰
8
+ api_key = os.getenv('OPENAI_API_KEY')
9
+ if not api_key:
10
+ raise ValueError("請設置 'OPENAI_API_KEY' 環境變數")
11
+
12
+ # OpenAI API key
13
+ openai_api_key = api_key
14
+
15
+ # 檢查是否為與調酒相關的問題
16
+ def is_related_to_bars(message):
17
+ keywords = [
18
+ '調酒', '酒吧', '酒類', 'cocktail', 'bartending', 'bar', 'alcohol', 'drink', # 調酒相關的關鍵字
19
+ '風味', '口味', '酸', '甜', '苦', '辣', 'flavor', 'taste', 'sour', 'sweet', 'bitter', 'spicy' # 風味和口味相關的關鍵字
20
+ ]
21
+ return any(keyword in message.lower() for keyword in keywords)
22
+
23
+ # 回應生成函數,使用 requests 來呼叫 OpenAI API
24
+ def response(message, history):
25
+ global conversation_history
26
+
27
+ # 檢查問題是否與調酒相關
28
+ if not is_related_to_bars(message):
29
+ print(f"問題與調酒無關: {message}")
30
+ return "抱歉,我只能回答與調酒、酒吧、酒類相關的問題。" # In Traditional Chinese
31
+
32
+ # 呼叫 OpenAI API
33
+ url = "https://api.openai.com/v1/chat/completions"
34
+ headers = {
35
+ "Content-Type": "application/json",
36
+ "Authorization": f"Bearer {openai_api_key}"
37
+ }
38
+
39
+ # 設置初始的 prompt_instruction
40
+ prompt_instruction = """
41
+ 你是李宗諺的專業小助教,名字叫做 '小李子',要以專業、冷淡但是非常有禮貌的口氣,與用戶互動並解答問題:
42
+ """
43
+ prompt_to_gpt = prompt_instruction + message
44
+
45
+ # 設置請求的數據
46
+ data = {
47
+ "model": "gpt-4o", # 使用 GPT-4 或 gpt-3.5-turbo
48
+ "messages": [{"role": "system", "content": prompt_to_gpt}],
49
+ "max_tokens": 200 # 控制生成的最大令牌數
50
+ }
51
+
52
+ try:
53
+ response = requests.post(url, headers=headers, data=json.dumps(data))
54
+ response.raise_for_status() # 檢查是否有錯誤
55
+ response_json = response.json()
56
+
57
+ # Debugging: 打印 API 回應
58
+ print("API Response:", response_json)
59
+
60
+ if 'choices' in response_json and len(response_json['choices']) > 0:
61
+ model_response = response_json['choices'][0]['message']['content']
62
+ return model_response
63
+ else:
64
+ return "Error: No response from the model."
65
+ except Exception as e:
66
+ print(f"API Error: {e}")
67
+ return f"API Error: {e}"
68
+
69
+ # 初始訊息
70
+ def initial_message():
71
+ return "我是你的專業調酒助理 小李子,我只能回答關於調酒的問題。" # In Traditional Chinese
72
+
73
+ # 建立 Gradio 聊天界面,並展示初始訊息
74
+ def create_demo():
75
+ with gr.Blocks() as demo:
76
+ chatbot = gr.ChatInterface(response, title='Lee Bartender Assistant', textbox=gr.Textbox(placeholder="Ask me about cocktails, bars, or drinks"))
77
+ gr.Markdown(initial_message()) # 初始訊息
78
+ return demo
79
+
80
+ demo = create_demo()
81
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ #requirements.txt
2
+ gradio
3
+ openai