SynthGen / app.py
ReallyFloppyPenguin's picture
Update app.py
5103a80 verified
raw
history blame
2.25 kB
import gradio as gr
from synthgen import generate_synthetic_text, api_key # Import from our modified synthgen
# Check if the API key was loaded successfully (provides feedback in Gradio UI)
api_key_loaded = True
def run_generation(prompt: str, model: str, num_samples: int) -> str:
"""
Wrapper function for Gradio interface to generate multiple samples.
"""
if not api_key_loaded:
return "Error: OPENROUTER_API_KEY not configured in Space secrets."
if not prompt:
return "Error: Please enter a prompt."
if num_samples <= 0:
return "Error: Number of samples must be positive."
output = f"Generating {num_samples} samples using model '{model}'...\n"
output += "="*20 + "\n\n"
for i in range(num_samples):
generated_text = generate_synthetic_text(prompt, model)
output += f"--- Sample {i+1} ---\n"
output += generated_text + "\n\n"
output += "="*20 + "\nGeneration complete."
return output
# --- Gradio Interface Definition ---
with gr.Blocks() as demo:
gr.Markdown("# Synthetic Text Generator using OpenRouter")
gr.Markdown(
"Generate multiple text samples based on a prompt using various models available on OpenRouter. "
"Ensure you have added your `OPENROUTER_API_KEY` to the Space secrets."
)
if not api_key_loaded:
gr.Markdown("**Warning:** `OPENROUTER_API_KEY` not found. Please add it to the Space secrets.")
with gr.Row():
prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here (e.g., Generate a short product description for a sci-fi gadget)")
with gr.Row():
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")
num_samples_input = gr.Number(label="Number of Samples", value=3, minimum=1, step=1)
generate_button = gr.Button("Generate Text")
output_text = gr.Textbox(label="Generated Samples", lines=15)
generate_button.click(
fn=run_generation,
inputs=[prompt_input, model_input, num_samples_input],
outputs=output_text
)
# Launch the Gradio app
if __name__ == "__main__":
demo.launch()