-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathchatbot.py
More file actions
45 lines (37 loc) · 1.22 KB
/
chatbot.py
File metadata and controls
45 lines (37 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import json
import requests
import gradio as gr
# Ollama local API endpoint
api = 'http://localhost:11434/api/chat'
def greet(query):
headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}
payload = {
"model": "qwen3:0.6b", # Adjust if you're using another local model
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": query}
]
}
response = requests.post(api, headers=headers, json=payload, stream=True)
result = ""
try:
for line in response.iter_lines():
if line:
data = json.loads(line.decode("utf-8"))
message = data.get("message", {}).get("content", "")
result += message
except Exception as e:
return f"❌ Error: {e}\n\nRaw response:\n{response.text if 'response' in locals() else ''}"
return result
# Gradio interface
demo = gr.Interface(
fn=greet,
inputs=gr.Textbox(label="Ask something..."),
outputs=gr.Textbox(label="Ollama says:"),
title="🐑 Ollama Chatbot",
description="Talk to your local llama! Powered by Ollama running locally."
)
demo.launch()