| import gradio as gr | |
| from transformers import pipeline | |
| # 1. Load the AI Brain (GPT-2) | |
| # We do this OUTSIDE the function so it only loads once when the app starts | |
| print("Loading AI...") | |
| generator = pipeline('text-generation', model='gpt2') | |
| # 2. Define the function | |
| def ask_brain(question): | |
| # Ask the AI to generate text | |
| result = generator(question, max_length=100, num_return_sequences=1) | |
| # Extract the text properly | |
| answer = result[0]['generated_text'] | |
| # Return the answer to the user | |
| return answer | |
| # 3. Create the App Interface | |
| iface = gr.Interface( | |
| fn=ask_brain, | |
| inputs="text", | |
| outputs="text", | |
| title="My Free AI Model", | |
| description="Ask me anything!" | |
| ) | |
| # 4. Launch the App | |
| iface.launch() | |