Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 2 |
+
import threading
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
model_name = "programordie2/trumpgpt"
|
| 6 |
+
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
def stream_generate(prompt):
|
| 11 |
+
if not prompt:
|
| 12 |
+
return
|
| 13 |
+
|
| 14 |
+
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
| 15 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True)
|
| 16 |
+
|
| 17 |
+
generation_kwargs = dict(input_ids=input_ids, max_new_tokens=50, streamer=streamer)
|
| 18 |
+
thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
|
| 19 |
+
thread.start()
|
| 20 |
+
|
| 21 |
+
generated_text = prompt
|
| 22 |
+
|
| 23 |
+
for text in streamer:
|
| 24 |
+
generated_text += text
|
| 25 |
+
yield generated_text
|
| 26 |
+
|
| 27 |
+
# Preset prompts
|
| 28 |
+
example_prompts = [
|
| 29 |
+
"The Fake News Media",
|
| 30 |
+
"Sleepy Joe Biden",
|
| 31 |
+
"MAKE AMERICA",
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
# Update input when a prompt is selected
|
| 35 |
+
def set_prompt(prompt):
|
| 36 |
+
return gr.update(elem_id="prompt-box", value=prompt)
|
| 37 |
+
|
| 38 |
+
# Interface with custom layout
|
| 39 |
+
with gr.Blocks(css="""
|
| 40 |
+
body { background-color: #f9f9f9; font-family: 'Segoe UI', sans-serif; }
|
| 41 |
+
.gradio-container { max-width: 700px; margin: auto; padding: 2em; }
|
| 42 |
+
textarea { font-size: 1rem !important; }
|
| 43 |
+
#output-box { white-space: pre-wrap; background: #222; border-radius: 12px; padding: 1em; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
|
| 44 |
+
""") as demo:
|
| 45 |
+
gr.Markdown("## ✨ TrumpGPT Playground")
|
| 46 |
+
gr.Markdown("TrumpGPT is a LLM based on GPT-2, trained on Donald Trump's tweets.")
|
| 47 |
+
gr.Markdown("Please note this is a next word predictor, not a chatbot.")
|
| 48 |
+
|
| 49 |
+
with gr.Column():
|
| 50 |
+
prompt_box = gr.Textbox(label="Prompt", lines=1, placeholder="Type the start of a sentence", elem_id="prompt-box")
|
| 51 |
+
|
| 52 |
+
gr.Markdown("No inspiration? Try one of these:")
|
| 53 |
+
|
| 54 |
+
for prompt in example_prompts:
|
| 55 |
+
btn = gr.Button(prompt, elem_id=f"prompt-{prompt}")
|
| 56 |
+
btn.click(set_prompt, btn, prompt_box, show_progress="hidden")
|
| 57 |
+
|
| 58 |
+
gr.Markdown("---")
|
| 59 |
+
|
| 60 |
+
generate_btn = gr.Button("Generate", variant="primary", elem_id="generate-btn")
|
| 61 |
+
output_box = gr.Textbox(label="Generated Text", lines=8, interactive=False, elem_id="output-box")
|
| 62 |
+
|
| 63 |
+
generate_btn.click(stream_generate, prompt_box, output_box)
|
| 64 |
+
|
| 65 |
+
demo.launch()
|