jljiu commited on
Commit
181ba1b
·
verified ·
1 Parent(s): dd53a96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -23
app.py CHANGED
@@ -2,10 +2,9 @@ import gradio as gr
2
  import requests
3
  import os
4
 
5
- # Ollama API地址 - 使用环境变量或默认值
6
- OLLAMA_API_URL = os.getenv('OLLAMA_API_URL', 'http://127.0.0.1:11434/api/generate')
7
 
8
- # 调用Ollama生成文本
9
  def generate_text(prompt):
10
  data = {
11
  "model": "llama3-zh",
@@ -13,34 +12,59 @@ def generate_text(prompt):
13
  "stream": False
14
  }
15
  try:
16
- # 添加更多的错误处理和日志
17
- print(f"Sending request to: {OLLAMA_API_URL}")
18
- print(f"Request data: {data}")
19
-
20
- response = requests.post(OLLAMA_API_URL, json=data, timeout=120)
21
- print(f"Response status: {response.status_code}")
22
- print(f"Response content: {response.text}")
23
-
24
- if response.status_code == 200:
25
- return response.json()["response"]
26
- else:
27
- return f"Error: {response.status_code} - {response.text}"
28
- except requests.exceptions.ConnectionError:
29
- return "Error: Could not connect to Ollama API. Please check if the service is running."
 
 
 
 
 
 
 
 
 
 
 
30
  except Exception as e:
31
- return f"Error: {str(e)}"
32
 
33
  # Gradio界面
34
  def chat_interface(prompt):
 
 
35
  return generate_text(prompt)
36
 
37
  # 创建Gradio应用
38
  iface = gr.Interface(
39
  fn=chat_interface,
40
- inputs="text",
41
- outputs="text",
 
 
 
 
 
 
 
42
  title="Llama3.1-8B-Chinese-Chat (CPU)",
43
- description="与 Llama3.1-8B-Chinese-Chat 模型对话(CPU 模式)"
 
 
 
 
 
44
  )
45
 
46
  # 启动应用
@@ -48,6 +72,5 @@ if __name__ == "__main__":
48
  iface.launch(
49
  server_name="0.0.0.0",
50
  server_port=7860,
51
- max_threads=1,
52
- share=False
53
  )
 
2
  import requests
3
  import os
4
 
5
+ # Ollama API地址 - 使用内部地址
6
+ OLLAMA_API_URL = "http://127.0.0.1:11434/api/generate"
7
 
 
8
  def generate_text(prompt):
9
  data = {
10
  "model": "llama3-zh",
 
12
  "stream": False
13
  }
14
  try:
15
+ # 添加重试机制
16
+ max_retries = 3
17
+ for attempt in range(max_retries):
18
+ try:
19
+ response = requests.post(
20
+ OLLAMA_API_URL,
21
+ json=data,
22
+ timeout=120,
23
+ proxies={'http': None, 'https': None} # 禁用代理
24
+ )
25
+ print(f"Attempt {attempt + 1}: Status {response.status_code}")
26
+
27
+ if response.status_code == 200:
28
+ return response.json()["response"]
29
+ elif response.status_code == 404:
30
+ return "错误:模型未找到,请等待模型加载完成后重试"
31
+ else:
32
+ if attempt == max_retries - 1:
33
+ return f"错误:{response.status_code} - {response.text}"
34
+ except requests.exceptions.ConnectionError:
35
+ if attempt == max_retries - 1:
36
+ return "错误:无法连接到 Ollama 服务"
37
+ print(f"连接失败,尝试重试 {attempt + 1}/{max_retries}")
38
+ time.sleep(2) # 等待2秒后重试
39
+
40
  except Exception as e:
41
+ return f"错误:{str(e)}"
42
 
43
  # Gradio界面
44
  def chat_interface(prompt):
45
+ if not prompt.strip():
46
+ return "请输入有效的问题"
47
  return generate_text(prompt)
48
 
49
  # 创建Gradio应用
50
  iface = gr.Interface(
51
  fn=chat_interface,
52
+ inputs=gr.Textbox(
53
+ lines=3,
54
+ placeholder="请输入您的问题...",
55
+ label="输入"
56
+ ),
57
+ outputs=gr.Textbox(
58
+ lines=5,
59
+ label="回答"
60
+ ),
61
  title="Llama3.1-8B-Chinese-Chat (CPU)",
62
+ description="与 Llama3.1-8B-Chinese-Chat 模型对话(CPU 模式)",
63
+ examples=[
64
+ ["你好,请做个自我介绍"],
65
+ ["解释一下量子计算的基本原理"],
66
+ ["写一首关于春天的诗"]
67
+ ]
68
  )
69
 
70
  # 启动应用
 
72
  iface.launch(
73
  server_name="0.0.0.0",
74
  server_port=7860,
75
+ max_threads=1
 
76
  )