Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# 1. Load a text-generation pipeline from Hugging Face
|
| 5 |
+
# (Choose any model from https://huggingface.co/models)
|
| 6 |
+
generator = pipeline(
|
| 7 |
+
"text-generation",
|
| 8 |
+
model="bigscience/bloom-560m", # Example smaller Bloom model
|
| 9 |
+
max_length=100
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
def chatbot(prompt):
|
| 13 |
+
"""
|
| 14 |
+
Generate a text completion from the hugging face model pipeline.
|
| 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 this Hugging Face model!"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
demo.launch()
|