Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +38 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
|
| 4 |
+
# Load pre-trained model and tokenizer
|
| 5 |
+
model_name = "gpt2" # You can also try "distilgpt2" for a smaller model
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
# Function to generate text
|
| 10 |
+
def generate_text(prompt, max_length=100, num_return_sequences=1):
|
| 11 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 12 |
+
outputs = model.generate(
|
| 13 |
+
inputs["input_ids"],
|
| 14 |
+
max_length=max_length,
|
| 15 |
+
num_return_sequences=num_return_sequences,
|
| 16 |
+
no_repeat_ngram_size=2,
|
| 17 |
+
top_p=0.95,
|
| 18 |
+
top_k=60,
|
| 19 |
+
temperature=0.7,
|
| 20 |
+
)
|
| 21 |
+
return [tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
|
| 22 |
+
|
| 23 |
+
# Create Gradio interface
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=generate_text,
|
| 26 |
+
inputs=[
|
| 27 |
+
gr.Textbox(label="Enter your prompt", placeholder="Type here..."),
|
| 28 |
+
gr.Slider(minimum=50, maximum=200, value=100, label="Max Length"),
|
| 29 |
+
gr.Slider(minimum=1, maximum=5, value=1, label="Number of Outputs")
|
| 30 |
+
],
|
| 31 |
+
outputs=gr.Textbox(label="Generated Text"),
|
| 32 |
+
title="GPT-2 Text Generator",
|
| 33 |
+
description="Generate human-like text based on your prompt using GPT-2.",
|
| 34 |
+
theme="compact",
|
| 35 |
+
examples=[["Once upon a time in a land far, far away..."]]
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|