Upload 2 files
Browse files- app.py +42 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
API_KEY = os.environ['API_KEY']
|
| 6 |
+
|
| 7 |
+
# Configure OpenAI client
|
| 8 |
+
client = OpenAI(
|
| 9 |
+
base_url="https://openrouter.ai/api/v1",
|
| 10 |
+
api_key="API_KEY",
|
| 11 |
+
default_headers={
|
| 12 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 13 |
+
"HTTP-Referer": "", # Optional: Your app's URL
|
| 14 |
+
"X-Title": "" # Optional: Your app name
|
| 15 |
+
}
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
messages = [
|
| 19 |
+
{"role": "system", "content": "Text Generation AI Assistant"},
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
def chatbot(input):
|
| 23 |
+
if input:
|
| 24 |
+
messages.append({"role": "user", "content": input})
|
| 25 |
+
response = client.chat.completions.create(
|
| 26 |
+
model="openai/gpt-oss-20b:free",
|
| 27 |
+
messages=messages
|
| 28 |
+
)
|
| 29 |
+
reply = response.choices[0].message.content
|
| 30 |
+
messages.append({"role": "assistant", "content": reply})
|
| 31 |
+
return reply
|
| 32 |
+
|
| 33 |
+
# Using modern Gradio interface
|
| 34 |
+
with gr.Interface(
|
| 35 |
+
fn=chatbot,
|
| 36 |
+
inputs=gr.Textbox(lines=3, label="Try: What is the value of pi", placeholder="Enter your message here..."),
|
| 37 |
+
outputs=gr.Textbox(lines=10,label="Response"),
|
| 38 |
+
title="",
|
| 39 |
+
description="",
|
| 40 |
+
theme=gr.themes.Default(primary_hue="sky")
|
| 41 |
+
) as demo:
|
| 42 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
openai
|