Spaces:
Running
Running
Update app.py
Browse files2 unmodified lines3456789144 unmodified lines1541551561571581591601 unmodified line16216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119212 unmodified lines2052062072082092102112122132142152162172181 unmodified line22022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025123 unmodified lines2752762772782792802812822832842852 unmodified linesimport tempfileimport osimport re # For parsing conversationfrom typing import Union, Optional, Dict # Import Dict# Import the actual functions from synthgenfrom synthgen import (generate_synthetic_text,144 unmodified lines# --- Modified Generation Wrappers ---# Wrapper for text generation + JSON preparation - RETURNS DICTdef run_generation_and_prepare_json(prompt: str,model: str,1 unmodified linetemperature: float,top_p: float,max_tokens: int) -> Dict[gr.Textbox, str]: # Return type hint (optional but good practice)"""Generates text samples and prepares a JSON file for download."""# Handle optional settingstemp_val = temperature if temperature > 0 else Nonetop_p_val = top_p if 0 < top_p <= 1 else Nonemax_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.if not prompt:# Return updates for both outputsreturn {output_text: gr.update(value="Error: Please enter a prompt."),download_file_text: gr.update(value=None) # Clear file output}if num_samples <= 0:return {output_text: gr.update(value="Error: Number of samples must be positive."),download_file_text: 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 linesoutput_str += "="*20 + "\nGeneration complete (check results above for errors)."json_filepath = create_json_file(results_list, "text_samples.json")# Return dictionary mapping components to updatesreturn {output_text: gr.update(value=output_str),download_file_text: gr.update(value=json_filepath) # Update file path}# Wrapper for conversation generation + JSON preparation - RETURNS DICTdef run_conversation_generation_and_prepare_json(system_prompts_text: str,model: str,1 unmodified linetemperature: float,top_p: float,max_tokens: int) -> Dict[gr.Textbox, str]: # Return type hint (optional)"""Generates conversations and prepares a JSON file for download."""temp_val = temperature if temperature > 0 else Nonetop_p_val = top_p if 0 < top_p <= 1 else Nonemax_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.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)}if num_turns <= 0:return {output_conv: gr.update(value="Error: Number of turns must be positive."),download_file_conv: 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)}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 linesoutput_str += "="*40 + "\nGeneration complete (check results above for errors)."json_filepath = create_json_file(results_list_structured, "conversations.json")# Return dictionary mapping components to updatesreturn {output_conv: gr.update(value=output_str),download_file_conv: gr.update(value=json_filepath)}# --- Gradio Interface Definition ---2 unmodified lines3456789144 unmodified lines1541551561571581591601 unmodified line16216316416516616716816917017117217317417517617717817912 unmodified lines1921931941951961971981992002012021 unmodified line20420520620720820921021121221321421521621721821922022122222322423 unmodified lines2482492502512522532542552 unmodified linesimport tempfileimport osimport re # For parsing conversationfrom 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 TUPLEdef run_generation_and_prepare_json(prompt: str,model: str,1 unmodified linetemperature: float,top_p: float,max_tokens: int) -> Tuple[gr.update, gr.update]: # Return type hint (optional)"""Generates text samples and prepares a JSON file for download."""# Handle optional settingstemp_val = temperature if temperature > 0 else Nonetop_p_val = top_p if 0 < top_p <= 1 else Nonemax_tokens_val = max_tokens if max_tokens > 0 else None# Handle errors by returning updates for both outputs in a tupleif not prompt:return (gr.update(value="Error: Please enter a prompt."), gr.update(value=None))if num_samples <= 0: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 linesoutput_str += "="*20 + "\nGeneration complete (check results above for errors)."json_filepath = create_json_file(results_list, "text_samples.json")# Return tuple of updates in the order of outputs listreturn (gr.update(value=output_str), gr.update(value=json_filepath))# Wrapper for conversation generation + JSON preparation - RETURNS TUPLEdef run_conversation_generation_and_prepare_json(system_prompts_text: str,model: str,1 unmodified linetemperature: float,top_p: float,max_tokens: int) -> 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 Nonetop_p_val = top_p if 0 < top_p <= 1 else Nonemax_tokens_val = max_tokens if max_tokens > 0 else None# Handle errors by returning updates for both outputs in a tupleif not system_prompts_text:return (gr.update(value="Error: Please enter or generate at least one system prompt/topic."), gr.update(value=None))if num_turns <= 0: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 (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 linesoutput_str += "="*40 + "\nGeneration complete (check results above for errors)."json_filepath = create_json_file(results_list_structured, "conversations.json")# Return tuple of updates in the order of outputs listreturn (gr.update(value=output_str), gr.update(value=json_filepath))# --- Gradio Interface Definition ---
app.py+16-46