Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -21,93 +21,87 @@ MODEL_OPTIONS = [
|
|
21 |
"liquid/lfm-40b:free"
|
22 |
]
|
23 |
|
24 |
-
#
|
25 |
history = []
|
26 |
|
27 |
-
def
|
28 |
global history
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
})
|
47 |
-
)
|
48 |
-
|
49 |
-
# Parse the response
|
50 |
-
if response.status_code == 200:
|
51 |
-
try:
|
52 |
-
response_json = response.json()
|
53 |
-
results[model] = response_json.get("choices", [{}])[0].get("message", {}).get("content", "No content returned.")
|
54 |
-
except json.JSONDecodeError:
|
55 |
-
results[model] = "Error: Unable to parse response."
|
56 |
-
else:
|
57 |
-
results[model] = f"Error: {response.status_code}, {response.text}"
|
58 |
|
59 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
history_entry = {
|
61 |
"input": input_text,
|
62 |
-
"
|
63 |
-
"
|
64 |
}
|
65 |
history.append(history_entry)
|
66 |
|
67 |
# Update the history state
|
68 |
history_state = history
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
def clear_history():
|
73 |
-
global history
|
74 |
-
history = []
|
75 |
-
return history
|
76 |
-
|
77 |
-
# Create Gradio interface with multiple model selection and history
|
78 |
-
with gr.Blocks() as demo:
|
79 |
-
input_text = gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here")
|
80 |
-
selected_models = gr.CheckboxGroup(choices=MODEL_OPTIONS, label="Select Models", value=[MODEL_OPTIONS[0]])
|
81 |
-
|
82 |
-
# Define output components with scrollable containers
|
83 |
-
output_comparisons = gr.JSON(label="Model Comparisons", elem_id="output-comparisons")
|
84 |
-
output_history = gr.JSON(label="History", elem_id="output-history")
|
85 |
|
86 |
-
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
|
113 |
-
|
|
|
21 |
"liquid/lfm-40b:free"
|
22 |
]
|
23 |
|
24 |
+
# Initialize history
|
25 |
history = []
|
26 |
|
27 |
+
def generate_text(input_text, selected_model, history_state):
|
28 |
global history
|
29 |
+
response = requests.post(
|
30 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
31 |
+
headers={
|
32 |
+
"Authorization": f"Bearer {API_KEY}",
|
33 |
+
"Content-Type": "application/json"
|
34 |
+
},
|
35 |
+
data=json.dumps({
|
36 |
+
"model": selected_model, # Use selected model
|
37 |
+
"messages": [{"role": "user", "content": input_text}],
|
38 |
+
"top_p": 1,
|
39 |
+
"temperature": 1,
|
40 |
+
"frequency_penalty": 0,
|
41 |
+
"presence_penalty": 0,
|
42 |
+
"repetition_penalty": 1,
|
43 |
+
"top_k": 0,
|
44 |
+
})
|
45 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
# Handle errors
|
48 |
+
if response.status_code != 200:
|
49 |
+
return f"Error: {response.status_code}, {response.text}", history_state
|
50 |
+
|
51 |
+
# Parse and return the content of the response
|
52 |
+
try:
|
53 |
+
response_json = response.json()
|
54 |
+
result = response_json.get("choices", [{}])[0].get("message", {}).get("content", "No content returned.")
|
55 |
+
except json.JSONDecodeError:
|
56 |
+
result = "Error: Unable to parse response."
|
57 |
+
|
58 |
+
# Add the current interaction to the history
|
59 |
history_entry = {
|
60 |
"input": input_text,
|
61 |
+
"selected_model": selected_model,
|
62 |
+
"response": result
|
63 |
}
|
64 |
history.append(history_entry)
|
65 |
|
66 |
# Update the history state
|
67 |
history_state = history
|
68 |
|
69 |
+
# Prepare the formatted history string
|
70 |
+
formatted_history = "\n".join([f"Input: {entry['input']}\nModel: {entry['selected_model']}\nResponse: {entry['response']}\n" for entry in history])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
return result, formatted_history
|
73 |
|
74 |
+
# Create Gradio interface with a dropdown for model selection
|
75 |
+
iface = gr.Interface(
|
76 |
+
fn=generate_text,
|
77 |
+
inputs=[
|
78 |
+
gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here"),
|
79 |
+
gr.Dropdown(choices=MODEL_OPTIONS, label="Select Model", value=MODEL_OPTIONS[0]),
|
80 |
+
gr.State()
|
81 |
+
],
|
82 |
+
outputs=[
|
83 |
+
gr.Textbox(label="Response", placeholder="Response will be shown here"),
|
84 |
+
gr.Textbox(label="History", placeholder="Interaction history will be shown here", lines=10, interactive=False)
|
85 |
+
],
|
86 |
+
title="Chat with OpenRouter Models"
|
87 |
+
)
|
88 |
|
89 |
+
# Insert custom CSS for scrollable sections
|
90 |
+
iface.add_component(gr.HTML("""
|
91 |
+
<style>
|
92 |
+
#output-comparisons {
|
93 |
+
height: 300px;
|
94 |
+
overflow: auto;
|
95 |
+
border: 1px solid #ddd;
|
96 |
+
padding: 10px;
|
97 |
+
}
|
98 |
+
#output-history {
|
99 |
+
height: 300px;
|
100 |
+
overflow: auto;
|
101 |
+
border: 1px solid #ddd;
|
102 |
+
padding: 10px;
|
103 |
+
}
|
104 |
+
</style>
|
105 |
+
"""))
|
106 |
|
107 |
+
iface.launch()
|