Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
+
|
4 |
+
# Load pretrained GPT-2 model and tokenizer
|
5 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
6 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
7 |
+
|
8 |
+
# Function to generate script
|
9 |
+
def generate_script(title):
|
10 |
+
# Add a context prompt to the title
|
11 |
+
prompt = "Title: " + title + "\n\nScript:"
|
12 |
+
|
13 |
+
# Tokenize the prompt
|
14 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt", max_length=1024, truncation=True)
|
15 |
+
|
16 |
+
# Generate text based on the prompt
|
17 |
+
output = model.generate(input_ids, max_length=200, num_return_sequences=1, temperature=0.7)
|
18 |
+
|
19 |
+
# Decode generated text and return
|
20 |
+
script = tokenizer.decode(output[0], skip_special_tokens=True)
|
21 |
+
return script
|
22 |
+
|
23 |
+
# Create Gradio interface
|
24 |
+
title_input = gr.inputs.Textbox(lines=2, label="Enter Title")
|
25 |
+
script_output = gr.outputs.Textbox(label="Generated Script")
|
26 |
+
|
27 |
+
gr.Interface(generate_script, title_input, script_output, title="Script Generator", description="Generate a script based on the provided title.").launch()
|