Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,10 +2,9 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import json
|
4 |
import os
|
5 |
-
import datetime
|
6 |
|
7 |
# Retrieve the OpenRouter API Key from the Space secrets
|
8 |
-
API_KEY = os.getenv("
|
9 |
|
10 |
# Define available models for selection
|
11 |
MODEL_OPTIONS = [
|
@@ -25,7 +24,7 @@ MODEL_OPTIONS = [
|
|
25 |
# History storage
|
26 |
history = []
|
27 |
|
28 |
-
def
|
29 |
global history
|
30 |
results = {}
|
31 |
for model in selected_models:
|
@@ -36,7 +35,7 @@ def generate_model_outputs_with_history(input_text, selected_models):
|
|
36 |
"Content-Type": "application/json"
|
37 |
},
|
38 |
data=json.dumps({
|
39 |
-
"model": model,
|
40 |
"messages": [{"role": "user", "content": input_text}],
|
41 |
"top_p": 1,
|
42 |
"temperature": 1,
|
@@ -46,6 +45,7 @@ def generate_model_outputs_with_history(input_text, selected_models):
|
|
46 |
"top_k": 0,
|
47 |
})
|
48 |
)
|
|
|
49 |
# Parse the response
|
50 |
if response.status_code == 200:
|
51 |
try:
|
@@ -60,78 +60,24 @@ def generate_model_outputs_with_history(input_text, selected_models):
|
|
60 |
history_entry = {
|
61 |
"input": input_text,
|
62 |
"selected_models": selected_models,
|
63 |
-
"outputs": results
|
64 |
-
"timestamp": str(datetime.datetime.now())
|
65 |
}
|
66 |
history.append(history_entry)
|
67 |
|
68 |
-
return results
|
69 |
-
|
70 |
-
# Create a dynamic number of outputs based on model selection
|
71 |
-
def create_outputs(selected_models):
|
72 |
-
output_components = []
|
73 |
-
for model in selected_models:
|
74 |
-
output_components.append(
|
75 |
-
gr.Textbox(
|
76 |
-
label=f"Output from {model}",
|
77 |
-
interactive=False,
|
78 |
-
lines=5, # Adjust lines as needed
|
79 |
-
max_lines=10, # Max lines before scrolling
|
80 |
-
show_label=False, # Hide label in the tabbed view
|
81 |
-
elem_id=f"output_{model}", # Unique ID for styling
|
82 |
-
css=".output-window { overflow-y: auto; max-height: 200px; }" # Styling for scrollable output
|
83 |
-
)
|
84 |
-
)
|
85 |
-
return output_components
|
86 |
-
|
87 |
-
def clear_history():
|
88 |
-
global history
|
89 |
-
history = []
|
90 |
-
return "History Cleared!", []
|
91 |
-
|
92 |
-
def export_history():
|
93 |
-
global history
|
94 |
-
# Save history to a file (e.g., JSON)
|
95 |
-
file_name = f"history_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
96 |
-
with open(file_name, 'w') as f:
|
97 |
-
json.dump(history, f, indent=4)
|
98 |
-
return f"History exported to {file_name}", []
|
99 |
-
|
100 |
-
# Gradio interface with dynamic outputs and history
|
101 |
-
with gr.Blocks() as demo:
|
102 |
-
with gr.Row():
|
103 |
-
input_text = gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here")
|
104 |
-
selected_models = gr.CheckboxGroup(choices=MODEL_OPTIONS, label="Select Models", value=[MODEL_OPTIONS[0]])
|
105 |
-
|
106 |
-
output_placeholder = gr.State([]) # Placeholder for dynamic output components
|
107 |
-
history_placeholder = gr.State(history) # Maintain history state
|
108 |
-
|
109 |
-
# Button to generate outputs and maintain history
|
110 |
-
generate_button = gr.Button("Generate Outputs")
|
111 |
-
|
112 |
-
def generate_and_update(input_text, selected_models):
|
113 |
-
# Generate the output and dynamically update the outputs
|
114 |
-
results = generate_model_outputs_with_history(input_text, selected_models)
|
115 |
-
output_components = create_outputs(selected_models)
|
116 |
-
|
117 |
-
# Update the output components with the actual model outputs
|
118 |
-
for i, model in enumerate(selected_models):
|
119 |
-
output_components[i].update(value=results.get(model, "No response"))
|
120 |
-
|
121 |
-
return output_components, results # Return dynamic outputs and results
|
122 |
-
|
123 |
-
generate_button.click(
|
124 |
-
fn=generate_and_update,
|
125 |
-
inputs=[input_text, selected_models],
|
126 |
-
outputs=[output_placeholder, history_placeholder]
|
127 |
-
)
|
128 |
-
|
129 |
-
# Clear History button
|
130 |
-
clear_history_button = gr.Button("Clear History")
|
131 |
-
clear_history_button.click(fn=clear_history, outputs=[gr.Textbox(value="History Cleared!"), history_placeholder])
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
-
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
import os
|
|
|
5 |
|
6 |
# Retrieve the OpenRouter API Key from the Space secrets
|
7 |
+
API_KEY = os.getenv("OpenRouter_API_KEY")
|
8 |
|
9 |
# Define available models for selection
|
10 |
MODEL_OPTIONS = [
|
|
|
24 |
# History storage
|
25 |
history = []
|
26 |
|
27 |
+
def generate_comparisons_with_history(input_text, selected_models):
|
28 |
global history
|
29 |
results = {}
|
30 |
for model in selected_models:
|
|
|
35 |
"Content-Type": "application/json"
|
36 |
},
|
37 |
data=json.dumps({
|
38 |
+
"model": model, # Use the current model
|
39 |
"messages": [{"role": "user", "content": input_text}],
|
40 |
"top_p": 1,
|
41 |
"temperature": 1,
|
|
|
45 |
"top_k": 0,
|
46 |
})
|
47 |
)
|
48 |
+
|
49 |
# Parse the response
|
50 |
if response.status_code == 200:
|
51 |
try:
|
|
|
60 |
history_entry = {
|
61 |
"input": input_text,
|
62 |
"selected_models": selected_models,
|
63 |
+
"outputs": results
|
|
|
64 |
}
|
65 |
history.append(history_entry)
|
66 |
|
67 |
+
return results, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
# Create Gradio interface with multiple model selection and history
|
70 |
+
iface = gr.Interface(
|
71 |
+
fn=generate_comparisons_with_history,
|
72 |
+
inputs=[
|
73 |
+
gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here"),
|
74 |
+
gr.CheckboxGroup(choices=MODEL_OPTIONS, label="Select Models", value=[MODEL_OPTIONS[0]])
|
75 |
+
],
|
76 |
+
outputs=[
|
77 |
+
gr.JSON(label="Model Comparisons"),
|
78 |
+
gr.JSON(label="History")
|
79 |
+
],
|
80 |
+
title="Compare Outputs and Maintain History"
|
81 |
+
)
|
82 |
|
83 |
+
iface.launch()
|