File size: 1,391 Bytes
c447887
 
 
 
 
 
 
 
 
e507614
c447887
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
import gradio as gr
import requests
import os

# Load API settings from environment variables
OPENWEBUI_URL = os.getenv("OPENWEBUI_URL")
OPENWEBUI_API_KEY = os.getenv("OPENWEBUI_API_KEY")
MODEL_NAME = os.getenv("MODEL_NAME", "phi3:latest")  # Default to "phi3:latest" if not set


# Function to send messages to OpenWebUI
def chat_with_model(message, history):
    if not OPENWEBUI_URL or not OPENWEBUI_API_KEY:
        return history + [("System", "Error: Missing API credentials. Please set environment variables.")]

    headers = {
        "Authorization": f"Bearer {OPENWEBUI_API_KEY}",
        "Content-Type": "application/json"
    }
    
    payload = {
        "model": MODEL_NAME,
        "messages": [{"role": "user", "content": message}]
    }
    
    response = requests.post(OPENWEBUI_URL, json=payload, headers=headers)
    
    if response.status_code == 200:
        bot_reply = response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response.")
    else:
        bot_reply = f"Error: {response.text}"
    
    history.append((message, bot_reply))
    return history

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## Chat with Ollama via OpenWebUI")
    chatbot = gr.Chatbot()
    msg = gr.Textbox(label="Your Message")
    submit = gr.Button("Send")

    submit.click(chat_with_model, inputs=[msg, chatbot], outputs=chatbot)

demo.launch()