mMonika commited on
Commit
573f259
·
verified ·
1 Parent(s): 5aa9f4a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -28
main.py CHANGED
@@ -1,33 +1,22 @@
1
- from fastapi import FastAPI
2
  import requests
3
- import os
4
- from fastapi.middleware.cors import CORSMiddleware
5
 
6
- app = FastAPI()
7
 
8
- # Allow frontend to connect
9
- app.add_middleware(
10
- CORSMiddleware,
11
- allow_origins=["*"],
12
- allow_credentials=True,
13
- allow_methods=["*"],
14
- allow_headers=["*"],
15
- )
16
-
17
- OLLAMA_BASE_URL = os.getenv("OLLAMA_URL", "http://localhost:11434")
18
 
19
- @app.get("/hi")
20
- def read_root():
21
- return {"message": "FastAPI with Ollama is running!"}
 
 
 
 
 
22
 
23
- @app.get("/ask_ollama/")
24
- def ask_ollama(question: str):
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)