Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,41 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
# Define the interface
|
| 7 |
interface = gr.Interface(
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
outputs="text",
|
| 13 |
-
css="""
|
| 14 |
-
.gr-form textarea {
|
| 15 |
-
height: 100px; /* Adjust height as needed */
|
| 16 |
-
}
|
| 17 |
-
""", # Optional: Adjust input text area height
|
| 18 |
-
description="Interact with BLOOM",
|
| 19 |
)
|
| 20 |
-
#max_token = gr.Slider(minimum=1, maximum=256, label="Max New Tokens", value=128),
|
| 21 |
|
| 22 |
# Launch the Gradio interface
|
| 23 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
# Define the BLOOM model name
|
| 6 |
+
model_name = "CreitinGameplays/bloom-3b-conversational"
|
| 7 |
+
|
| 8 |
+
# Load tokenizer and model
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 11 |
+
|
| 12 |
+
def generate_text(prompt):
|
| 13 |
+
"""Generates text using the BLOOM model from Hugging Face Transformers."""
|
| 14 |
+
# Encode the prompt into tokens
|
| 15 |
+
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
| 16 |
+
|
| 17 |
+
# Generate text with the prompt and limit the maximum length to 256 tokens
|
| 18 |
+
output = model.generate(
|
| 19 |
+
input_ids=input_ids,
|
| 20 |
+
max_length=256,
|
| 21 |
+
num_return_sequences=1, # Generate only 1 sequence
|
| 22 |
+
do_sample=True, # Enable sampling for creativity
|
| 23 |
+
top_k=50, # Sample from the top 50 most likely tokens at each step
|
| 24 |
+
top_p=0.95, # Filter out highly probable unlikely continuations
|
| 25 |
+
temperature=1.0 # Control the randomness of the generated text (1.0 for default)
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
# Decode the generated token sequence back to text
|
| 29 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 30 |
+
return generated_text
|
| 31 |
|
| 32 |
+
# Define the Gradio interface
|
| 33 |
interface = gr.Interface(
|
| 34 |
+
fn=generate_text,
|
| 35 |
+
inputs="text",
|
| 36 |
+
outputs="text",
|
| 37 |
+
description="Interact with BLOOM (Loaded with Hugging Face Transformers)",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
|
|
|
| 39 |
|
| 40 |
# Launch the Gradio interface
|
| 41 |
+
interface.launch()
|