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