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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -1,27 +1,30 @@
1
  import gradio as gr
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")
 
 
 
 
 
25
 
26
- # 启动 Gradio 应用
27
- iface.launch()
 
1
  import gradio as gr
2
  import requests
3
 
4
+ # 设置 API 地址
5
+ OLLAMA_API_URL = "http://127.0.0.1:7860" # 这是内部 API 地址,仅作示例
6
 
7
  # 定义请求 Ollama API 的函数
8
  def get_ollama_response(query):
9
  try:
10
+ # 发送请求到内部 API
11
+ response = requests.post(f"{OLLAMA_API_URL}/api/query", 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
  return response_data.get("answer", "No answer found in response.")
18
  except Exception as e:
19
+ return f"Error: {str(e)}"
 
20
 
21
  # 使用 Gradio 创建界面
22
+ iface = gr.Interface(
23
+ fn=get_ollama_response, # 直接连接到处理函数
24
+ inputs="text",
25
+ outputs="text",
26
+ title="Ollama API Demo",
27
+ )
28
 
29
+ # 启动 Gradio 应用,指定 Hugging Face Spaces 的端口
30
+ iface.launch(server_name="0.0.0.0", server_port=7860)