Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, set_seed
|
| 3 |
-
import torch
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
os.makedirs(cache_dir, exist_ok=True)
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
generator = pipeline(
|
| 12 |
-
|
| 13 |
-
model=
|
| 14 |
-
|
| 15 |
-
device=0 if torch.cuda.is_available() else -1
|
| 16 |
)
|
| 17 |
|
| 18 |
-
def generate_text(prompt, max_length, num_return_sequences, seed):
|
| 19 |
-
"""Generate text with
|
| 20 |
-
|
| 21 |
-
try:
|
| 22 |
-
set_seed(int(seed)) # Handle user-provided seed
|
| 23 |
-
except ValueError:
|
| 24 |
-
pass # Ignore invalid seeds
|
| 25 |
-
|
| 26 |
-
# Generate with sampling for better diversity
|
| 27 |
results = generator(
|
| 28 |
prompt,
|
| 29 |
-
max_length=
|
| 30 |
-
num_return_sequences=
|
| 31 |
-
|
| 32 |
pad_token_id=generator.tokenizer.eos_token_id
|
| 33 |
)
|
| 34 |
-
|
| 35 |
# Format results with numbering
|
| 36 |
-
|
| 37 |
-
for i, res in enumerate(results):
|
| 38 |
-
output += f"🔥 Result {i+1}:\n{res['generated_text']}\n\n{'-'*30}\n"
|
| 39 |
-
return output
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
with gr.Blocks(theme="soft") as demo:
|
| 43 |
gr.Markdown("""
|
| 44 |
-
# GPT-1 Text
|
| 45 |
-
|
| 46 |
-
Generate text using the original OpenAI GPT model (2018).
|
| 47 |
-
""")
|
| 48 |
|
| 49 |
-
with
|
| 50 |
-
|
| 51 |
-
label="Enter your prompt",
|
| 52 |
-
placeholder="Once upon a time...",
|
| 53 |
-
lines=3
|
| 54 |
-
)
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
value=3,
|
| 68 |
-
step=1,
|
| 69 |
-
label="Number of Results"
|
| 70 |
-
)
|
| 71 |
-
seed = gr.Textbox(
|
| 72 |
-
value="42",
|
| 73 |
-
label="Seed (leave blank for random)",
|
| 74 |
-
max_lines=1
|
| 75 |
-
)
|
| 76 |
|
| 77 |
-
generate_btn = gr.Button("Generate Text"
|
| 78 |
-
output = gr.Textbox(label="Generated
|
| 79 |
|
| 80 |
generate_btn.click(
|
| 81 |
fn=generate_text,
|
| 82 |
inputs=[prompt, max_length, num_return_sequences, seed],
|
| 83 |
outputs=output
|
| 84 |
)
|
| 85 |
-
|
| 86 |
-
gr.Markdown("""
|
| 87 |
-
### Tips for Better Results
|
| 88 |
-
- Use clear, specific prompts
|
| 89 |
-
- Increase max length for longer stories
|
| 90 |
-
- Try different seeds for varied outputs
|
| 91 |
-
- GPT-1 has significant limitations compared to modern models
|
| 92 |
-
""")
|
| 93 |
|
| 94 |
-
|
| 95 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, set_seed
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Set environment variables for CPU optimization
|
| 6 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
|
|
|
| 7 |
|
| 8 |
+
# Load model once at startup
|
| 9 |
generator = pipeline(
|
| 10 |
+
"text-generation",
|
| 11 |
+
model="openai-community/openai-gpt",
|
| 12 |
+
device=-1 # Auto-detect CPU
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
+
def generate_text(prompt, max_length=30, num_return_sequences=3, seed=42):
|
| 16 |
+
"""Generate text with configurable parameters and seed"""
|
| 17 |
+
set_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
results = generator(
|
| 19 |
prompt,
|
| 20 |
+
max_length=max_length,
|
| 21 |
+
num_return_sequences=num_return_sequences,
|
| 22 |
+
truncation=True,
|
| 23 |
pad_token_id=generator.tokenizer.eos_token_id
|
| 24 |
)
|
|
|
|
| 25 |
# Format results with numbering
|
| 26 |
+
return "\n\n".join([f"{i+1}. {r['generated_text']}" for i, r in enumerate(results)])
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# Define UI
|
| 29 |
+
with gr.Blocks(theme="soft", title="GPT-1 Text Generator") as demo:
|
| 30 |
gr.Markdown("""
|
| 31 |
+
# 📜 GPT-1 Text Generation Demo
|
| 32 |
+
Run on free HuggingFace CPU • Model: [openai-community/openai-gpt](https://huggingface.co/openai-community/openai-gpt )
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
*Note: This is the original 2018 GPT model with known limitations and biases. For production use, consider newer models.*
|
| 35 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
with gr.Row():
|
| 38 |
+
with gr.Column():
|
| 39 |
+
prompt = gr.Textbox(
|
| 40 |
+
label="Input Prompt",
|
| 41 |
+
placeholder="Enter your prompt here...",
|
| 42 |
+
lines=4
|
| 43 |
+
)
|
| 44 |
+
seed = gr.Number(value=42, label="Random Seed")
|
| 45 |
+
|
| 46 |
+
with gr.Column():
|
| 47 |
+
max_length = gr.Slider(10, 100, value=30, step=5, label="Max Output Length")
|
| 48 |
+
num_return_sequences = gr.Slider(1, 5, value=3, step=1, label="Number of Variants")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
generate_btn = gr.Button("✨ Generate Text")
|
| 51 |
+
output = gr.Textbox(label="Generated Results", lines=10)
|
| 52 |
|
| 53 |
generate_btn.click(
|
| 54 |
fn=generate_text,
|
| 55 |
inputs=[prompt, max_length, num_return_sequences, seed],
|
| 56 |
outputs=output
|
| 57 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
demo.launch()
|