Spaces:
Runtime error
Runtime error
Update app.py
Browse files52 unmodified lines5354555657585913 unmodified lines7374757677787980818283841 unmodified line86878889909192936 unmodified lines10010110210310410510610710810911011111211311411511652 unmodified linesdef generate_text(prompt, max_length=100, temperature=0.7, top_k=50):# Initialize if not already doneif MODEL is None:initialize()try:# Process prompt13 unmodified linesoutputs = MODEL.generate(input_ids,max_length=min(max_length + len(input_ids[0]), 2048),temperature=temperature,top_k=top_k,do_sample=True,pad_token_id=TOKENIZER.pad_token_id,eos_token_id=TOKENIZER.eos_token_id,num_return_sequences=1)# Decode and return1 unmodified linereturn generated_text.strip()except Exception as e:print(f"Error generating text: {str(e)}")return f"An error occurred: {str(e)}"# Create Gradio interfaceiface = gr.Interface(6 unmodified lines],outputs=gr.Textbox(label="Generated Text", lines=5),title="SmolLM2 Text Generator",description="""Generate text using the fine-tuned SmolLM2 model.- Max Length: Controls the length of generated text- Temperature: Controls randomness (higher = more creative)- Top K: Controls diversity of word choices""",examples=[["Once upon a time", 100, 0.7, 50],["The quick brown fox", 150, 0.8, 40],["In a galaxy far far away", 200, 0.9, 30],],allow_flagging="never")if __name__ == "__main__":iface.launch()52 unmodified lines5354555657585960616213 unmodified lines7677787980818283848586871 unmodified line8990919293949596976 unmodified lines10410510610710810911011111211311411511611711811912012112212352 unmodified linesdef generate_text(prompt, max_length=100, temperature=0.7, top_k=50):# Initialize if not already doneif MODEL is None:try:initialize()except Exception as e:return f"Failed to initialize model: {str(e)}"try:# Process prompt13 unmodified linesoutputs = MODEL.generate(input_ids,max_length=min(max_length + len(input_ids[0]), 2048),temperature=max(0.1, min(temperature, 1.0)), # Clamp temperaturetop_k=max(1, min(top_k, 100)), # Clamp top_kdo_sample=True if temperature > 0 else False,num_return_sequences=1,pad_token_id=TOKENIZER.pad_token_id,eos_token_id=TOKENIZER.eos_token_id,)# Decode and return1 unmodified linereturn generated_text.strip()except Exception as e:import tracebacktraceback.print_exc()return f"Error during text generation: {str(e)}"# Create Gradio interfaceiface = gr.Interface(6 unmodified lines],outputs=gr.Textbox(label="Generated Text", lines=5),title="SmolLM2 Text Generator",description="Generate text using the fine-tuned SmolLM2 model. Adjust parameters to control the generation.",examples=[["Once upon a time", 100, 0.7, 50],["The quick brown fox", 150, 0.8, 40],],allow_flagging="never")# Initialize on startuptry:initialize()except Exception as e:print(f"Warning: Model initialization failed: {str(e)}")print("Model will be initialized on first request")if __name__ == "__main__":iface.launch()
app.py+19-12