Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Load API settings from environment variables
|
6 |
+
OPENWEBUI_URL = os.getenv("OPENWEBUI_URL")
|
7 |
+
OPENWEBUI_API_KEY = os.getenv("OPENWEBUI_API_KEY")
|
8 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "phi3:latest") # Default to "phi3:latest" if not set
|
9 |
+
|
10 |
+
# Function to send messages to OpenWebUI
|
11 |
+
def chat_with_model(message, history):
|
12 |
+
if not OPENWEBUI_URL or not OPENWEBUI_API_KEY:
|
13 |
+
return history + [("System", "Error: Missing API credentials. Please set environment variables.")]
|
14 |
+
|
15 |
+
headers = {
|
16 |
+
"Authorization": f"Bearer {OPENWEBUI_API_KEY}",
|
17 |
+
"Content-Type": "application/json"
|
18 |
+
}
|
19 |
+
|
20 |
+
payload = {
|
21 |
+
"model": MODEL_NAME,
|
22 |
+
"messages": [{"role": "user", "content": message}]
|
23 |
+
}
|
24 |
+
|
25 |
+
response = requests.post(OPENWEBUI_URL, json=payload, headers=headers)
|
26 |
+
|
27 |
+
if response.status_code == 200:
|
28 |
+
bot_reply = response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response.")
|
29 |
+
else:
|
30 |
+
bot_reply = f"Error: {response.text}"
|
31 |
+
|
32 |
+
history.append((message, bot_reply))
|
33 |
+
return history
|
34 |
+
|
35 |
+
# Gradio UI
|
36 |
+
with gr.Blocks() as demo:
|
37 |
+
gr.Markdown("## Chat with Ollama via OpenWebUI")
|
38 |
+
chatbot = gr.Chatbot()
|
39 |
+
msg = gr.Textbox(label="Your Message")
|
40 |
+
submit = gr.Button("Send")
|
41 |
+
|
42 |
+
submit.click(chat_with_model, inputs=[msg, chatbot], outputs=chatbot)
|
43 |
+
|
44 |
+
demo.launch()
|