Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines34566789144 unmodified lines1541551561571571581591601 unmodified line16216316416516516616716816917017117217317417517617717817217318018118218318417417518618718818917617717817912 unmodified lines1921931942082092102112121951961971982151992002012021 unmodified line20420520622320720820921021121222923023121321423323423523621521623823924024121721821922024524624724822122222322423 unmodified lines2482492502782792802812822512522532542552 unmodified linesimport tempfileimport osimport re # For parsing conversationfrom typing import Union, Optional, Dict # Import Dictfrom typing import Union, Optional, Dict, Tuple # Import Dict and Tuple# Import the actual functions from synthgenfrom synthgen import ( generate_synthetic_text,144 unmodified lines
# --- Modified Generation Wrappers ---
# Wrapper for text generation + JSON preparation - RETURNS DICT# Wrapper for text generation + JSON preparation - RETURNS TUPLEdef run_generation_and_prepare_json( prompt: str, model: str,1 unmodified line temperature: float, top_p: float, max_tokens: int) -> Dict[gr.Textbox, str]: # Return type hint (optional but good practice)) -> Tuple[gr.update, gr.update]: # Return type hint (optional) """Generates text samples and prepares a JSON file for download.""" # Handle optional settings temp_val = temperature if temperature > 0 else None top_p_val = top_p if 0 < top_p <= 1 else None max_tokens_val = max_tokens if max_tokens > 0 else None
# Define component objects used in return dict keys - MUST MATCH OUTPUTS # This requires the components to be defined *before* this function, # which isn't the case. So we cannot use component objects as keys here. # Gradio handles mapping if the keys are strings matching component labels # OR if we return gr.update targeting components. # Let's return explicit gr.update for clarity and robustness.
# Handle errors by returning updates for both outputs in a tuple if not prompt: # Return updates for both outputs return { output_text: gr.update(value="Error: Please enter a prompt."), download_file_text: gr.update(value=None) # Clear file output } return (gr.update(value="Error: Please enter a prompt."), gr.update(value=None)) if num_samples <= 0: return { output_text: gr.update(value="Error: Number of samples must be positive."), download_file_text: gr.update(value=None) } return (gr.update(value="Error: Number of samples must be positive."), gr.update(value=None))
output_str = f"Generating {num_samples} samples using model '{model}'...\n" output_str += f"(Settings: Temp={temp_val}, Top-P={top_p_val}, MaxTokens={max_tokens_val})\n"12 unmodified lines output_str += "="*20 + "\nGeneration complete (check results above for errors)." json_filepath = create_json_file(results_list, "text_samples.json")
# Return dictionary mapping components to updates return { output_text: gr.update(value=output_str), download_file_text: gr.update(value=json_filepath) # Update file path } # Return tuple of updates in the order of outputs list return (gr.update(value=output_str), gr.update(value=json_filepath))
# Wrapper for conversation generation + JSON preparation - RETURNS DICT# Wrapper for conversation generation + JSON preparation - RETURNS TUPLEdef run_conversation_generation_and_prepare_json( system_prompts_text: str, model: str,1 unmodified line temperature: float, top_p: float, max_tokens: int) -> Dict[gr.Textbox, str]: # Return type hint (optional)) -> Tuple[gr.update, gr.update]: # Return type hint (optional) """Generates conversations and prepares a JSON file for download.""" temp_val = temperature if temperature > 0 else None top_p_val = top_p if 0 < top_p <= 1 else None max_tokens_val = max_tokens if max_tokens > 0 else None
# Define component objects used in return dict keys - requires components defined first. # Using explicit gr.update instead.
# Handle errors by returning updates for both outputs in a tuple if not system_prompts_text: return { output_conv: gr.update(value="Error: Please enter or generate at least one system prompt/topic."), download_file_conv: gr.update(value=None) } return (gr.update(value="Error: Please enter or generate at least one system prompt/topic."), gr.update(value=None)) if num_turns <= 0: return { output_conv: gr.update(value="Error: Number of turns must be positive."), download_file_conv: gr.update(value=None) } return (gr.update(value="Error: Number of turns must be positive."), gr.update(value=None))
prompts = [p.strip() for p in system_prompts_text.strip().split('\n') if p.strip()] if not prompts: return { output_conv: gr.update(value="Error: No valid prompts found in the input."), download_file_conv: gr.update(value=None) } return (gr.update(value="Error: No valid prompts found in the input."), gr.update(value=None))
output_str = f"Generating {len(prompts)} conversations ({num_turns} turns each) using model '{model}'...\n" output_str += f"(Settings: Temp={temp_val}, Top-P={top_p_val}, MaxTokens={max_tokens_val})\n"23 unmodified lines output_str += "="*40 + "\nGeneration complete (check results above for errors)." json_filepath = create_json_file(results_list_structured, "conversations.json")
# Return dictionary mapping components to updates return { output_conv: gr.update(value=output_str), download_file_conv: gr.update(value=json_filepath) } # Return tuple of updates in the order of outputs list return (gr.update(value=output_str), gr.update(value=json_filepath))
# --- Gradio Interface Definition ---app.py+16-46