Update app.py
Browse files
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" #
|
| 6 |
|
| 7 |
# 定义请求 Ollama API 的函数
|
| 8 |
def get_ollama_response(query):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|