Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,11 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 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="qwen/qwen3-coder: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: Python code for Fibonacci series", 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 textgeneration:
|
| 42 |
-
textgeneration.launch()
|
|
|
|
| 1 |
+
_C='API_KEY'
|
| 2 |
+
_B='content'
|
| 3 |
+
_A='role'
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
import gradio as gr,os
|
| 6 |
+
API_KEY=os.environ[_C]
|
| 7 |
+
client=OpenAI(base_url='https://openrouter.ai/api/v1',api_key=_C,default_headers={'Authorization':f"Bearer {API_KEY}",'HTTP-Referer':'','X-Title':''})
|
| 8 |
+
messages=[{_A:'system',_B:'Text Generation AI Assistant'}]
|
| 9 |
+
def chatbot(input):
|
| 10 |
+
if input:messages.append({_A:'user',_B:input});B=client.chat.completions.create(model='qwen/qwen3-coder:free',messages=messages);A=B.choices[0].message.content;messages.append({_A:'assistant',_B:A});return A
|
| 11 |
+
with gr.Interface(fn=chatbot,inputs=gr.Textbox(lines=3,label='Try: Python code for Fibonacci series',placeholder='Enter your message here...'),outputs=gr.Textbox(lines=10,label='Response'),title='',description='',theme=gr.themes.Default(primary_hue='sky'))as textgeneration:textgeneration.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|