javaeeduke commited on
Commit
9e07eb7
·
verified ·
1 Parent(s): b5bfde5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -1,20 +1,29 @@
1
  import os
2
- import gradio as block
3
  import gradio as gr
4
  from openai import OpenAI
5
 
6
- # 1. 从环境变量读取 FreeLLMAPI 的配置
7
- FREE_LLM_API_URL = os.getenv("FREE_LLM_API_URL", "https://你的FreeLLMAPI地址.hf.space/v1")
8
- FREE_LLM_API_KEY = os.getenv("FREE_LLM_API_KEY", "your_api_key_here")
 
9
 
10
- # 2. 初始化 OpenAI 客户端桥接 FreeLLMAPI
 
 
 
 
 
11
  client = OpenAI(
12
  base_url=FREE_LLM_API_URL,
13
  api_key=FREE_LLM_API_KEY
14
  )
15
 
16
  def predict(message, history):
17
- # 将 Gradio 的历史记录转换为 OpenAI 格式
 
 
 
 
18
  history_openai = []
19
  for human, ai in history:
20
  history_openai.append({"role": "user", "content": human})
@@ -22,11 +31,10 @@ def predict(message, history):
22
  history_openai.append({"role": "user", "content": message})
23
 
24
  try:
25
- # 调用 FreeLLMAPI 聚合的模型(例如 gemini)
26
  response = client.chat.completions.create(
27
  model="gemini",
28
  messages=history_openai,
29
- stream=True # 开启流式传输,体验更好
30
  )
31
 
32
  partial_message = ""
@@ -36,19 +44,17 @@ def predict(message, history):
36
  yield partial_message
37
 
38
  except Exception as e:
39
- yield f"⚠️ 链接 FreeLLMAPI 出: {str(e)}"
 
40
 
41
- # 3. 极简的 Gradio 聊天界面
42
  demo = gr.ChatInterface(
43
  fn=predict,
44
- title="AI 自动化助手 (Gradio + FreeLLMAPI)",
45
  textbox=gr.Textbox(placeholder="请输入内容...", container=False, scale=7)
46
  )
47
 
48
  if __name__ == "__main__":
49
- # 注意:在 Docker 环境下,server_name 必须是 "0.0.0.0"
50
- # 这里我们直接读取 Dockerfile 里配置好的环境变量
51
  server_name = os.getenv("GRADIO_SERVER_NAME", "0.0.0.0")
52
  server_port = int(os.getenv("GRADIO_SERVER_PORT", 7860))
53
-
54
  demo.queue().launch(server_name=server_name, server_port=server_port)
 
1
  import os
 
2
  import gradio as gr
3
  from openai import OpenAI
4
 
5
+ # ================= 🔐 安全隐藏区域 =================
6
+ # 核心:只读取变量名,绝不在这里写死任何真实的 URL 或 KEY
7
+ FREE_LLM_API_URL = os.getenv("FREE_LLM_API_URL")
8
+ FREE_LLM_API_KEY = os.getenv("FREE_LLM_API_KEY")
9
 
10
+ # 启动前做个基础的安全检查在容器日志中提示,但不会暴露具体内容
11
+ if not FREE_LLM_API_URL or not FREE_LLM_API_KEY:
12
+ print("⚠️ 警告: 环境变量 FREE_LLM_API_URL 或 FREE_LLM_API_KEY 未配置,API 链接可能会失败!")
13
+ # ==================================================
14
+
15
+ # 初始化 OpenAI 客户端(桥接 FreeLLMAPI)
16
  client = OpenAI(
17
  base_url=FREE_LLM_API_URL,
18
  api_key=FREE_LLM_API_KEY
19
  )
20
 
21
  def predict(message, history):
22
+ # 再次确保密匙存在才执行
23
+ if not client.api_key or not client.base_url:
24
+ yield "❌ 系统未配置 API 密钥,请在平台后台设置 Environment Variables / Secrets。"
25
+ return
26
+
27
  history_openai = []
28
  for human, ai in history:
29
  history_openai.append({"role": "user", "content": human})
 
31
  history_openai.append({"role": "user", "content": message})
32
 
33
  try:
 
34
  response = client.chat.completions.create(
35
  model="gemini",
36
  messages=history_openai,
37
+ stream=True
38
  )
39
 
40
  partial_message = ""
 
44
  yield partial_message
45
 
46
  except Exception as e:
47
+ # 安全提示:报时模糊处理,防止由于报错信息太详细而泄露了敏感的 URL
48
+ yield f"⚠️ 接口连接失败,请检查后台配置。"
49
 
50
+ # Gradio 界面
51
  demo = gr.ChatInterface(
52
  fn=predict,
53
+ title="AI 自动化助手",
54
  textbox=gr.Textbox(placeholder="请输入内容...", container=False, scale=7)
55
  )
56
 
57
  if __name__ == "__main__":
 
 
58
  server_name = os.getenv("GRADIO_SERVER_NAME", "0.0.0.0")
59
  server_port = int(os.getenv("GRADIO_SERVER_PORT", 7860))
 
60
  demo.queue().launch(server_name=server_name, server_port=server_port)