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 = [
|
|
@@ -60,8 +59,7 @@ 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 |
|
|
@@ -69,31 +67,7 @@ def generate_model_outputs_with_history(input_text, selected_models):
|
|
| 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:
|
|
@@ -104,9 +78,8 @@ with gr.Blocks() as demo:
|
|
| 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(
|
| 108 |
-
#
|
| 109 |
-
selected_models = selected_models
|
| 110 |
output_components = create_outputs(selected_models)
|
| 111 |
output_placeholder.set(output_components)
|
| 112 |
return output_components
|
|
@@ -115,32 +88,12 @@ with gr.Blocks() as demo:
|
|
| 115 |
generate_button = gr.Button("Generate Outputs")
|
| 116 |
generate_button.click(
|
| 117 |
fn=generate_model_outputs_with_history,
|
| 118 |
-
inputs=[input_text, selected_models],
|
| 119 |
-
outputs=[update_outputs_and_history(
|
| 120 |
)
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
with gr.
|
| 124 |
-
|
| 125 |
-
gr.JSON(label="History", value=history_placeholder)
|
| 126 |
-
for model in MODEL_OPTIONS:
|
| 127 |
-
with gr.Tab(f"{model} Output"):
|
| 128 |
-
gr.Textbox(
|
| 129 |
-
label=f"Output from {model}",
|
| 130 |
-
interactive=False,
|
| 131 |
-
lines=5, # Adjust lines as needed
|
| 132 |
-
max_lines=10, # Max lines before scrolling
|
| 133 |
-
show_label=False, # Hide label in the tabbed view
|
| 134 |
-
elem_id=f"output_{model}", # Unique ID for styling
|
| 135 |
-
css=".output-window { overflow-y: auto; max-height: 200px; }" # Styling for scrollable output
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
# Clear History button
|
| 139 |
-
clear_history_button = gr.Button("Clear History")
|
| 140 |
-
clear_history_button.click(fn=clear_history, outputs=[gr.Textbox(value="History Cleared!"), history_placeholder])
|
| 141 |
-
|
| 142 |
-
# Export History button
|
| 143 |
-
export_history_button = gr.Button("Export History")
|
| 144 |
-
export_history_button.click(fn=export_history, outputs=[gr.Textbox(value="History Exported Successfully!")])
|
| 145 |
|
| 146 |
demo.launch()
|
|
|
|
| 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 = [
|
|
|
|
| 59 |
history_entry = {
|
| 60 |
"input": input_text,
|
| 61 |
"selected_models": selected_models,
|
| 62 |
+
"outputs": results
|
|
|
|
| 63 |
}
|
| 64 |
history.append(history_entry)
|
| 65 |
|
|
|
|
| 67 |
|
| 68 |
# Create a dynamic number of outputs based on model selection
|
| 69 |
def create_outputs(selected_models):
|
| 70 |
+
return [gr.Textbox(label=f"Output from {model}", interactive=False) for model in selected_models]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# Gradio interface with dynamic outputs and history
|
| 73 |
with gr.Blocks() as demo:
|
|
|
|
| 78 |
output_placeholder = gr.State([]) # Placeholder for dynamic output components
|
| 79 |
history_placeholder = gr.State(history) # Maintain history state
|
| 80 |
|
| 81 |
+
def update_outputs_and_history(selected_models):
|
| 82 |
+
# Dynamically create outputs for each selected model
|
|
|
|
| 83 |
output_components = create_outputs(selected_models)
|
| 84 |
output_placeholder.set(output_components)
|
| 85 |
return output_components
|
|
|
|
| 88 |
generate_button = gr.Button("Generate Outputs")
|
| 89 |
generate_button.click(
|
| 90 |
fn=generate_model_outputs_with_history,
|
| 91 |
+
inputs=[input_text, selected_models],
|
| 92 |
+
outputs=[update_outputs_and_history(selected_models), history_placeholder],
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Display the history
|
| 96 |
+
with gr.Row():
|
| 97 |
+
gr.JSON(label="History", value=history_placeholder)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
demo.launch()
|