Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,107 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
-
message,
|
12 |
-
history:
|
13 |
-
system_message,
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
26 |
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
gr.Slider(
|
53 |
minimum=0.1,
|
54 |
maximum=1.0,
|
55 |
value=0.95,
|
56 |
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)"
|
58 |
),
|
59 |
],
|
|
|
|
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from openai import OpenAI
|
4 |
+
from typing import List, Tuple
|
5 |
|
6 |
+
# Initialize OpenRouter client
|
7 |
+
client = OpenAI(
|
8 |
+
base_url="https://openrouter.ai/api/v1",
|
9 |
+
api_key=os.getenv("OPENROUTER_API_KEY")
|
10 |
+
)
|
11 |
|
12 |
+
# Available models
|
13 |
+
MODELS = [
|
14 |
+
"nousresearch/hermes-3-llama-3.1-405b",
|
15 |
+
"anthropic/claude-3-opus",
|
16 |
+
"google/gemma-7b",
|
17 |
+
"mistralai/mixtral-8x7b"
|
18 |
+
]
|
19 |
|
20 |
def respond(
|
21 |
+
message: str,
|
22 |
+
history: List[Tuple[str, str]],
|
23 |
+
system_message: str,
|
24 |
+
model: str,
|
25 |
+
max_tokens: int,
|
26 |
+
temperature: float,
|
27 |
+
top_p: float,
|
28 |
+
) -> str:
|
29 |
+
# Construct messages array with system message and history
|
30 |
messages = [{"role": "system", "content": system_message}]
|
31 |
+
|
32 |
+
# Add conversation history
|
33 |
+
for user_msg, assistant_msg in history:
|
34 |
+
if user_msg:
|
35 |
+
messages.append({"role": "user", "content": user_msg})
|
36 |
+
if assistant_msg:
|
37 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
38 |
+
|
39 |
+
# Add current message
|
40 |
messages.append({"role": "user", "content": message})
|
41 |
+
|
42 |
+
try:
|
43 |
+
completion = client.chat.completions.create(
|
44 |
+
extra_headers={
|
45 |
+
"HTTP-Referer": os.getenv("SITE_URL", "https://huggingface.co/spaces"),
|
46 |
+
"X-Title": os.getenv("SITE_NAME", "OpenRouter Gradio Interface"),
|
47 |
+
},
|
48 |
+
model=model,
|
49 |
+
messages=messages,
|
50 |
+
max_tokens=max_tokens,
|
51 |
+
temperature=temperature,
|
52 |
+
top_p=top_p,
|
53 |
+
stream=True # Enable streaming
|
54 |
+
)
|
55 |
+
|
56 |
+
# Stream the response
|
57 |
+
response = ""
|
58 |
+
for chunk in completion:
|
59 |
+
if chunk.choices[0].delta.content is not None:
|
60 |
+
response += chunk.choices[0].delta.content
|
61 |
+
yield response
|
62 |
+
|
63 |
+
except Exception as e:
|
64 |
+
yield f"Error: {str(e)}"
|
65 |
|
66 |
+
# Create Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
demo = gr.ChatInterface(
|
68 |
respond,
|
69 |
additional_inputs=[
|
70 |
+
gr.Textbox(
|
71 |
+
value="You are a helpful and friendly AI assistant.",
|
72 |
+
label="System message"
|
73 |
+
),
|
74 |
+
gr.Dropdown(
|
75 |
+
choices=MODELS,
|
76 |
+
value=MODELS[0],
|
77 |
+
label="Model"
|
78 |
+
),
|
79 |
+
gr.Slider(
|
80 |
+
minimum=1,
|
81 |
+
maximum=2048,
|
82 |
+
value=512,
|
83 |
+
step=1,
|
84 |
+
label="Max new tokens"
|
85 |
+
),
|
86 |
+
gr.Slider(
|
87 |
+
minimum=0.1,
|
88 |
+
maximum=2.0,
|
89 |
+
value=0.7,
|
90 |
+
step=0.1,
|
91 |
+
label="Temperature"
|
92 |
+
),
|
93 |
gr.Slider(
|
94 |
minimum=0.1,
|
95 |
maximum=1.0,
|
96 |
value=0.95,
|
97 |
step=0.05,
|
98 |
+
label="Top-p (nucleus sampling)"
|
99 |
),
|
100 |
],
|
101 |
+
title="OpenRouter Chat Interface",
|
102 |
+
description="Chat with various AI models through OpenRouter API"
|
103 |
)
|
104 |
|
105 |
+
# Launch the app
|
106 |
if __name__ == "__main__":
|
107 |
+
demo.launch()
|