Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,30 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
|
4 |
-
# 设置
|
5 |
-
OLLAMA_API_URL = "http://
|
6 |
|
7 |
# 定义请求 Ollama API 的函数
|
8 |
def get_ollama_response(query):
|
9 |
try:
|
10 |
-
#
|
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 |
-
|
21 |
-
return "Error: Unable to process the request."
|
22 |
|
23 |
# 使用 Gradio 创建界面
|
24 |
-
iface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
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)
|