Spaces:
Sleeping
Sleeping
File size: 2,248 Bytes
9a73686 5103a80 9a73686 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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() |