Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
4 |
+
|
5 |
+
# Load the model and tokenizer
|
6 |
+
model = GPT2LMHeadModel.from_pretrained("samwell/SamGPT")
|
7 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2") # Assuming you used the GPT-2 tokenizer
|
8 |
+
|
9 |
+
def generate_text(prompt, max_length):
|
10 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
11 |
+
output = model.generate(input_ids, max_length=max_length, num_return_sequences=1, no_repeat_ngram_size=2)
|
12 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
13 |
+
return generated_text
|
14 |
+
|
15 |
+
iface = gr.Interface(
|
16 |
+
fn=generate_text,
|
17 |
+
inputs=[
|
18 |
+
gr.Textbox(label="Prompt"),
|
19 |
+
gr.Slider(minimum=10, maximum=200, value=50, step=1, label="Max Length")
|
20 |
+
],
|
21 |
+
outputs=gr.Textbox(label="Generated Text"),
|
22 |
+
title="SamGPT Text Generation",
|
23 |
+
description="Enter a prompt to generate text with SamGPT."
|
24 |
+
)
|
25 |
+
|
26 |
+
iface.launch()
|