Delete app.py
Browse files
app.py
DELETED
@@ -1,109 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import gradio as gr
|
3 |
-
from openai import OpenAI
|
4 |
-
from dotenv import load_dotenv
|
5 |
-
|
6 |
-
# Load environment variables from .env file (for local testing)
|
7 |
-
load_dotenv()
|
8 |
-
|
9 |
-
# Get API key from environment variables
|
10 |
-
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
11 |
-
|
12 |
-
# Initialize OpenAI client with OpenRouter base URL
|
13 |
-
client = OpenAI(
|
14 |
-
base_url="https://openrouter.ai/api/v1",
|
15 |
-
api_key=OPENROUTER_API_KEY
|
16 |
-
)
|
17 |
-
|
18 |
-
# List of available models - customize as needed
|
19 |
-
# You can get the full list from the OpenRouter API
|
20 |
-
AVAILABLE_MODELS = [
|
21 |
-
"openai/gpt-3.5-turbo",
|
22 |
-
"openai/gpt-4",
|
23 |
-
"anthropic/claude-3-opus",
|
24 |
-
"anthropic/claude-3-sonnet",
|
25 |
-
"meta-llama/llama-3-70b-instruct",
|
26 |
-
"google/gemini-pro",
|
27 |
-
"deepseek/deepseek-chat-v3-0324:free"
|
28 |
-
]
|
29 |
-
|
30 |
-
def generate_response(message, model_name, system_prompt, temperature=0.7):
|
31 |
-
"""Generate a response using the selected model via OpenRouter."""
|
32 |
-
if not message:
|
33 |
-
return "Please enter a message to get a response."
|
34 |
-
|
35 |
-
try:
|
36 |
-
response = client.chat.completions.create(
|
37 |
-
model=model_name,
|
38 |
-
messages=[
|
39 |
-
{"role": "system", "content": system_prompt},
|
40 |
-
{"role": "user", "content": message}
|
41 |
-
],
|
42 |
-
temperature=temperature
|
43 |
-
)
|
44 |
-
return response.choices[0].message.content
|
45 |
-
except Exception as e:
|
46 |
-
return f"Error: {str(e)}"
|
47 |
-
|
48 |
-
# Create Gradio interface
|
49 |
-
with gr.Blocks() as demo:
|
50 |
-
gr.Markdown("# OpenRouter Chat Interface")
|
51 |
-
|
52 |
-
with gr.Row():
|
53 |
-
with gr.Column(scale=3):
|
54 |
-
model_dropdown = gr.Dropdown(
|
55 |
-
choices=AVAILABLE_MODELS,
|
56 |
-
value=AVAILABLE_MODELS[0],
|
57 |
-
label="Select Model"
|
58 |
-
)
|
59 |
-
|
60 |
-
system_prompt = gr.Textbox(
|
61 |
-
value="You are a helpful AI assistant.",
|
62 |
-
label="System Prompt",
|
63 |
-
lines=2
|
64 |
-
)
|
65 |
-
|
66 |
-
temperature_slider = gr.Slider(
|
67 |
-
minimum=0.1,
|
68 |
-
maximum=1.0,
|
69 |
-
value=0.7,
|
70 |
-
step=0.1,
|
71 |
-
label="Temperature"
|
72 |
-
)
|
73 |
-
|
74 |
-
with gr.Row():
|
75 |
-
with gr.Column(scale=4):
|
76 |
-
user_input = gr.Textbox(
|
77 |
-
label="Your Message",
|
78 |
-
placeholder="Type your message here...",
|
79 |
-
lines=3
|
80 |
-
)
|
81 |
-
|
82 |
-
with gr.Row():
|
83 |
-
submit_btn = gr.Button("Send")
|
84 |
-
|
85 |
-
with gr.Row():
|
86 |
-
with gr.Column(scale=4):
|
87 |
-
output = gr.Textbox(label="Response", lines=8)
|
88 |
-
|
89 |
-
submit_btn.click(
|
90 |
-
fn=generate_response,
|
91 |
-
inputs=[user_input, model_dropdown, system_prompt, temperature_slider],
|
92 |
-
outputs=output
|
93 |
-
)
|
94 |
-
|
95 |
-
# Also allow sending by pressing Enter in the input box
|
96 |
-
user_input.submit(
|
97 |
-
fn=generate_response,
|
98 |
-
inputs=[user_input, model_dropdown, system_prompt, temperature_slider],
|
99 |
-
outputs=output
|
100 |
-
)
|
101 |
-
|
102 |
-
gr.Markdown("""
|
103 |
-
## About
|
104 |
-
This interface uses [OpenRouter](https://openrouter.ai/) to access various AI models.
|
105 |
-
""")
|
106 |
-
|
107 |
-
# Launch the app
|
108 |
-
if __name__ == "__main__":
|
109 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|