Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,25 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
# (Choose any model from https://huggingface.co/models)
|
| 6 |
generator = pipeline(
|
| 7 |
"text-generation",
|
| 8 |
-
model="
|
| 9 |
-
max_length=100
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
def chatbot(prompt):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
""
|
| 16 |
-
# 2. Pass the prompt to the pipeline
|
| 17 |
-
responses = generator(prompt, max_length=150, num_return_sequences=1)
|
| 18 |
-
# The pipeline returns a list of dicts; we only need the generated_text
|
| 19 |
-
generated_text = responses[0]["generated_text"]
|
| 20 |
-
|
| 21 |
-
# Optionally, if you want to remove the original prompt portion from the output, you can slice it out.
|
| 22 |
-
# For simplicity, we'll just return the entire generated text.
|
| 23 |
-
return generated_text
|
| 24 |
|
| 25 |
-
# 3. Create and launch the Gradio interface
|
| 26 |
demo = gr.Interface(
|
| 27 |
fn=chatbot,
|
| 28 |
inputs="text",
|
| 29 |
outputs="text",
|
| 30 |
-
title="Hugging Face Chatbot",
|
| 31 |
-
description="Ask anything to
|
| 32 |
)
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Create text-generation pipeline with explicit truncation.
|
|
|
|
| 5 |
generator = pipeline(
|
| 6 |
"text-generation",
|
| 7 |
+
model="gpt2",
|
| 8 |
+
max_length=100,
|
| 9 |
+
truncation=True # important
|
| 10 |
)
|
| 11 |
|
| 12 |
def chatbot(prompt):
|
| 13 |
+
# Generate text with the pipeline, also specifying truncation at call time (optional).
|
| 14 |
+
responses = generator(prompt, max_length=100, truncation=True)
|
| 15 |
+
return responses[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
| 17 |
demo = gr.Interface(
|
| 18 |
fn=chatbot,
|
| 19 |
inputs="text",
|
| 20 |
outputs="text",
|
| 21 |
+
title="Hugging Face GPT-2 Chatbot",
|
| 22 |
+
description="Ask anything to the GPT-2-based model!"
|
| 23 |
)
|
| 24 |
|
| 25 |
if __name__ == "__main__":
|