Marathon23 commited on
Commit
82525ae
1 Parent(s): 952b474

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -2
app.py CHANGED
@@ -1,4 +1,3 @@
1
- #app.py
2
  import time
3
  import gradio as gr
4
  import google.generativeai as genai
@@ -20,6 +19,14 @@ try:
20
  except Exception as e:
21
  raise ValueError(f"無法載入模型:{e}")
22
 
 
 
 
 
 
 
 
 
23
  # 將 Gradio 的歷史紀錄轉換為 Gemini 格式
24
  def transform_history(history):
25
  new_history = []
@@ -31,6 +38,12 @@ def transform_history(history):
31
  # 回應生成函數
32
  def response(message, history):
33
  global chat
 
 
 
 
 
 
34
  # 將 Gradio 的歷史紀錄轉換為 Gemini 的格式
35
  chat.history = transform_history(history)
36
 
@@ -46,4 +59,4 @@ def response(message, history):
46
  # 建立 Gradio 聊天界面
47
  gr.ChatInterface(response,
48
  title='Gemini Chat',
49
- textbox=gr.Textbox(placeholder="Question to Gemini")).launch(share=True)
 
 
1
  import time
2
  import gradio as gr
3
  import google.generativeai as genai
 
19
  except Exception as e:
20
  raise ValueError(f"無法載入模型:{e}")
21
 
22
+ # 定義與酒類、調酒、酒吧相關的關鍵詞
23
+ keywords = ["alcohol", "cocktail", "beer", "wine", "whiskey", "vodka", "rum", "bartender", "bar", "drink", "liquor", "spirits"]
24
+
25
+ # 檢查訊息是否與酒類、調酒、酒吧相關
26
+ def is_relevant_question(message):
27
+ message_lower = message.lower() # 將訊息轉為小寫以便檢查
28
+ return any(keyword in message_lower for keyword in keywords)
29
+
30
  # 將 Gradio 的歷史紀錄轉換為 Gemini 格式
31
  def transform_history(history):
32
  new_history = []
 
38
  # 回應生成函數
39
  def response(message, history):
40
  global chat
41
+
42
+ # 檢查訊息是否與酒類、調酒、酒吧相關
43
+ if not is_relevant_question(message):
44
+ yield "I'm sorry, I can only answer questions related to alcohol, cocktails, and bars."
45
+ return
46
+
47
  # 將 Gradio 的歷史紀錄轉換為 Gemini 的格式
48
  chat.history = transform_history(history)
49
 
 
59
  # 建立 Gradio 聊天界面
60
  gr.ChatInterface(response,
61
  title='Gemini Chat',
62
+ textbox=gr.Textbox(placeholder="Question to Gemini")).launch(share=True)