Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#1.Load the AI Brain (
|
|
|
|
| 5 |
print("Loading AI...")
|
| 6 |
generator = pipeline('text-generation', model='gpt2')
|
| 7 |
|
| 8 |
-
#2.Define the function
|
| 9 |
def ask_brain(question):
|
| 10 |
-
#
|
| 11 |
-
result = generator(question, max_length
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# 1. Load the AI Brain (GPT-2)
|
| 5 |
+
# We do this OUTSIDE the function so it only loads once when the app starts
|
| 6 |
print("Loading AI...")
|
| 7 |
generator = pipeline('text-generation', model='gpt2')
|
| 8 |
|
| 9 |
+
# 2. Define the function
|
| 10 |
def ask_brain(question):
|
| 11 |
+
# Ask the AI to generate text
|
| 12 |
+
result = generator(question, max_length=100, num_return_sequences=1)
|
| 13 |
+
|
| 14 |
+
# Extract the text properly
|
| 15 |
+
answer = result[0]['generated_text']
|
| 16 |
+
|
| 17 |
+
# Return the answer to the user
|
| 18 |
+
return answer
|
| 19 |
|
| 20 |
+
# 3. Create the App Interface
|
| 21 |
+
iface = gr.Interface(
|
| 22 |
+
fn=ask_brain,
|
| 23 |
+
inputs="text",
|
| 24 |
+
outputs="text",
|
| 25 |
+
title="My Free AI Model",
|
| 26 |
+
description="Ask me anything!"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# 4. Launch the App
|
| 30 |
+
iface.launch()
|