nikunjcepatel's picture
Update app.py
3aea094 verified
raw
history blame
938 Bytes
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()