Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from synthgen import generate_synthetic_text, api_key # Import from our modified synthgen
|
3 |
+
|
4 |
+
# Check if the API key was loaded successfully (provides feedback in Gradio UI)
|
5 |
+
api_key_loaded = bool(api_key)
|
6 |
+
|
7 |
+
def run_generation(prompt: str, model: str, num_samples: int) -> str:
|
8 |
+
"""
|
9 |
+
Wrapper function for Gradio interface to generate multiple samples.
|
10 |
+
"""
|
11 |
+
if not api_key_loaded:
|
12 |
+
return "Error: OPENROUTER_API_KEY not configured in Space secrets."
|
13 |
+
if not prompt:
|
14 |
+
return "Error: Please enter a prompt."
|
15 |
+
if num_samples <= 0:
|
16 |
+
return "Error: Number of samples must be positive."
|
17 |
+
|
18 |
+
output = f"Generating {num_samples} samples using model '{model}'...\n"
|
19 |
+
output += "="*20 + "\n\n"
|
20 |
+
|
21 |
+
for i in range(num_samples):
|
22 |
+
generated_text = generate_synthetic_text(prompt, model)
|
23 |
+
output += f"--- Sample {i+1} ---\n"
|
24 |
+
output += generated_text + "\n\n"
|
25 |
+
|
26 |
+
output += "="*20 + "\nGeneration complete."
|
27 |
+
return output
|
28 |
+
|
29 |
+
# --- Gradio Interface Definition ---
|
30 |
+
with gr.Blocks() as demo:
|
31 |
+
gr.Markdown("# Synthetic Text Generator using OpenRouter")
|
32 |
+
gr.Markdown(
|
33 |
+
"Generate multiple text samples based on a prompt using various models available on OpenRouter. "
|
34 |
+
"Ensure you have added your `OPENROUTER_API_KEY` to the Space secrets."
|
35 |
+
)
|
36 |
+
if not api_key_loaded:
|
37 |
+
gr.Markdown("**Warning:** `OPENROUTER_API_KEY` not found. Please add it to the Space secrets.")
|
38 |
+
|
39 |
+
with gr.Row():
|
40 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here (e.g., Generate a short product description for a sci-fi gadget)")
|
41 |
+
with gr.Row():
|
42 |
+
model_input = gr.Textbox(label="OpenRouter Model ID", value="deepseek/deepseek-chat-v3-0324:free", placeholder="e.g., openai/gpt-3.5-turbo, google/gemini-flash-1.5")
|
43 |
+
num_samples_input = gr.Number(label="Number of Samples", value=3, minimum=1, step=1)
|
44 |
+
|
45 |
+
generate_button = gr.Button("Generate Text")
|
46 |
+
output_text = gr.Textbox(label="Generated Samples", lines=15)
|
47 |
+
|
48 |
+
generate_button.click(
|
49 |
+
fn=run_generation,
|
50 |
+
inputs=[prompt_input, model_input, num_samples_input],
|
51 |
+
outputs=output_text
|
52 |
+
)
|
53 |
+
|
54 |
+
# Launch the Gradio app
|
55 |
+
if __name__ == "__main__":
|
56 |
+
demo.launch()
|