Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -1,33 +1,22 @@
|
|
1 |
-
|
2 |
import requests
|
3 |
-
import os
|
4 |
-
from fastapi.middleware.cors import CORSMiddleware
|
5 |
|
6 |
-
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
allow_methods=["*"],
|
14 |
-
allow_headers=["*"],
|
15 |
-
)
|
16 |
-
|
17 |
-
OLLAMA_BASE_URL = os.getenv("OLLAMA_URL", "http://localhost:11434")
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
payload = {
|
26 |
-
"model": "mistral",
|
27 |
-
"messages": [
|
28 |
-
{"role": "system", "content": "You are an AI assistant."},
|
29 |
-
{"role": "user", "content": question}
|
30 |
-
]
|
31 |
-
}
|
32 |
-
response = requests.post(f"{OLLAMA_BASE_URL}/api/generate", json=payload)
|
33 |
-
return response.json()
|
|
|
1 |
+
import gradio as gr
|
2 |
import requests
|
|
|
|
|
3 |
|
4 |
+
FASTAPI_URL = "http://localhost:7860/ask_ollama/"
|
5 |
|
6 |
+
def ask_ollama(question):
|
7 |
+
response = requests.get(f"{FASTAPI_URL}?question={question}")
|
8 |
+
if response.status_code == 200:
|
9 |
+
return response.json().get("response", "No response from Ollama.")
|
10 |
+
return "Error: Could not connect to API."
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Create Gradio interface
|
13 |
+
iface = gr.Interface(
|
14 |
+
fn=ask_ollama,
|
15 |
+
inputs=gr.Textbox(placeholder="Ask a question..."),
|
16 |
+
outputs="text",
|
17 |
+
title="Ollama Chat",
|
18 |
+
description="Ask anything and get responses from the Ollama model."
|
19 |
+
)
|
20 |
|
21 |
+
# Run on port 7861 (so FastAPI and Gradio can run together)
|
22 |
+
iface.launch(server_name="0.0.0.0", server_port=7861, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|