Spaces:
Sleeping
Sleeping
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 = [
|
@@ -59,15 +60,40 @@ def generate_model_outputs_with_history(input_text, selected_models):
|
|
59 |
history_entry = {
|
60 |
"input": input_text,
|
61 |
"selected_models": selected_models,
|
62 |
-
"outputs": results
|
|
|
63 |
}
|
64 |
history.append(history_entry)
|
65 |
|
66 |
-
return
|
67 |
|
68 |
# Create a dynamic number of outputs based on model selection
|
69 |
def create_outputs(selected_models):
|
70 |
-
return [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Gradio interface with dynamic outputs and history
|
73 |
with gr.Blocks() as demo:
|
@@ -78,22 +104,26 @@ 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
|
86 |
-
|
87 |
# Button to generate outputs and maintain history
|
88 |
generate_button = gr.Button("Generate Outputs")
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
generate_button.click(
|
90 |
-
fn=
|
91 |
inputs=[input_text, selected_models],
|
92 |
-
outputs=[
|
93 |
)
|
94 |
|
95 |
-
#
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
98 |
|
99 |
demo.launch()
|
|
|
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 = [
|
|
|
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 # Return the results for the 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 |
output_placeholder = gr.State([]) # Placeholder for dynamic output components
|
105 |
history_placeholder = gr.State(history) # Maintain history state
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
# Button to generate outputs and maintain history
|
108 |
generate_button = gr.Button("Generate Outputs")
|
109 |
+
|
110 |
+
def generate_and_update(input_text, selected_models):
|
111 |
+
results = generate_model_outputs_with_history(input_text, selected_models)
|
112 |
+
output_components = create_outputs(selected_models)
|
113 |
+
return output_components, results
|
114 |
+
|
115 |
generate_button.click(
|
116 |
+
fn=generate_and_update,
|
117 |
inputs=[input_text, selected_models],
|
118 |
+
outputs=[output_placeholder, history_placeholder]
|
119 |
)
|
120 |
|
121 |
+
# Clear History button
|
122 |
+
clear_history_button = gr.Button("Clear History")
|
123 |
+
clear_history_button.click(fn=clear_history, outputs=[gr.Textbox(value="History Cleared!"), history_placeholder])
|
124 |
+
|
125 |
+
# Export History button
|
126 |
+
export_history_button = gr.Button("Export History")
|
127 |
+
export_history_button.click(fn=export_history, outputs=[gr.Textbox(value="History Exported Successfully!")])
|
128 |
|
129 |
demo.launch()
|