import gradio as gr from transformers import pipeline # Load a text-generation model. # For demonstration, we're using GPT-2. # For an adult-novel style, consider a fine-tuned model or adjust parameters. generator = pipeline("text-generation", model="gpt2") def generate_text(prompt, max_length): try: # Generate text based on the prompt. results = generator(prompt, max_length=max_length, do_sample=True, temperature=0.8) return results[0]["generated_text"] except Exception as e: # Log the exception details for debugging print("Error during text generation:", e) # Return an error message for the user return "An error occurred while generating text. Please try again with a shorter prompt or lower max length." # Create the Gradio interface. iface = gr.Interface( fn=generate_text, inputs=[ gr.Textbox(lines=5, label="Enter your prompt here..."), gr.Slider(minimum=50, maximum=500, step=10, value=200, label="Max Length") ], outputs="text", title="Adult Novel Generator", description=( "This application generates text in the style of an adult novel. " "Provide a prompt to start the narrative and see the generated continuation." ) ) # Launch the application. if __name__ == "__main__": iface.launch()