huduo commited on
Commit
9c8e686
·
verified ·
1 Parent(s): cd963d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -2,13 +2,23 @@ import gradio as gr
2
  import requests
3
 
4
  # 设置 Ollama API 地址
5
- OLLAMA_API_URL = "http://localhost:7860" # 这个是你本地 Ollama 服务的 URL
6
 
7
  # 定义请求 Ollama API 的函数
8
  def get_ollama_response(query):
9
- # 发送请求到 Ollama API
10
- response = requests.post(OLLAMA_API_URL, json={"query": query})
11
- return response.json()["answer"] # 假设返回的 JSON 格式包含 "answer"
 
 
 
 
 
 
 
 
 
 
12
 
13
  # 使用 Gradio 创建界面
14
  iface = gr.Interface(fn=get_ollama_response, inputs="text", outputs="text")
 
2
  import requests
3
 
4
  # 设置 Ollama API 地址
5
+ OLLAMA_API_URL = "http://localhost:7860/api/query" # 替换为实际路径
6
 
7
  # 定义请求 Ollama API 的函数
8
  def get_ollama_response(query):
9
+ try:
10
+ # 发送请求到 Ollama API
11
+ response = requests.post(OLLAMA_API_URL, json={"query": query})
12
+ if response.status_code != 200:
13
+ return f"Error: Server returned status code {response.status_code}"
14
+
15
+ # 解析 JSON 响应
16
+ response_data = response.json()
17
+ print("API Response:", response_data) # 调试时查看完整响应
18
+ return response_data.get("answer", "No answer found in response.")
19
+ except Exception as e:
20
+ print("Error:", e)
21
+ return "Error: Unable to process the request."
22
 
23
  # 使用 Gradio 创建界面
24
  iface = gr.Interface(fn=get_ollama_response, inputs="text", outputs="text")