Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,31 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 6 |
def generate_text(input_text):
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
demo = gr.Interface(fn=generate_text, inputs="text", outputs="text")
|
| 10 |
demo.launch()
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
# Load model safely
|
| 5 |
+
try:
|
| 6 |
+
model = pipeline("text-generation", model="ButteredToast/ToastBot")
|
| 7 |
+
except Exception as e:
|
| 8 |
+
print(f"Error loading model: {e}")
|
| 9 |
+
model = None # Prevent crashes
|
| 10 |
|
| 11 |
+
# Define text generation function
|
| 12 |
def generate_text(input_text):
|
| 13 |
+
if model is None:
|
| 14 |
+
return "Error: Model could not be loaded. Check your Hugging Face space logs."
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
output = model(input_text, max_length=100) # Control output length
|
| 18 |
+
return output[0]["generated_text"]
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"Error generating text: {e}"
|
| 21 |
+
|
| 22 |
+
# Set up Gradio interface
|
| 23 |
+
demo = gr.Interface(
|
| 24 |
+
fn=generate_text,
|
| 25 |
+
inputs="text",
|
| 26 |
+
outputs="text",
|
| 27 |
+
title="ToastBot AI",
|
| 28 |
+
description="Chat with ToastBot, your AI assistant!"
|
| 29 |
+
)
|
| 30 |
|
|
|
|
| 31 |
demo.launch()
|