File size: 938 Bytes
66ff3e9
 
b944409
 
 
 
66ff3e9
 
 
3aea094
66ff3e9
 
 
c307a1d
 
 
66ff3e9
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import os

# Retrieve the Open Router API Key from the Space secrets
API_KEY = os.getenv("OpenRounter_API_KEY")

def chat_with_openrouter(prompt):
    url = "https://api.openrouter.ai/v1/chat/completions"
    headers = {"Authorization": "Bearer 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"}
    data = {
        "model": "openai/gpt-4o-mini-2024-07-18",
        "messages": [{"role": "user", "content": prompt}],
        "top_p": 1,
        "temperature": 1,
        "repetition_penalty": 1,
        "transforms": []
    }
    response = requests.post(url, headers=headers, json=data)
    return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")

iface = gr.Interface(
    fn=chat_with_openrouter,
    inputs=gr.Textbox(lines=2, placeholder="Ask me anything"),
    outputs="text",
    title="Chat with OpenRouter",
)

iface.launch()