Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 4 |
+
|
| 5 |
+
# Load the GPT-2 model and tokenizer
|
| 6 |
+
model_name = "openai-community/gpt2"
|
| 7 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
| 8 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
def generate_transcript(input_text):
|
| 11 |
+
# Split the input text into smaller parts
|
| 12 |
+
sentences = input_text.split('. ') # Simple split by sentences
|
| 13 |
+
new_transcripts = []
|
| 14 |
+
|
| 15 |
+
# Generate new transcript for each chunk
|
| 16 |
+
for sentence in sentences:
|
| 17 |
+
input_ids = tokenizer.encode(sentence, return_tensors='pt')
|
| 18 |
+
|
| 19 |
+
# Generate new text
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
output = model.generate(input_ids, max_length=50, num_return_sequences=1)
|
| 22 |
+
|
| 23 |
+
new_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 24 |
+
new_transcripts.append(new_text)
|
| 25 |
+
|
| 26 |
+
# Combine all output to make the final transcript
|
| 27 |
+
final_transcript = '. '.join(new_transcripts)
|
| 28 |
+
|
| 29 |
+
return final_transcript
|
| 30 |
+
|
| 31 |
+
# Create the Gradio interface
|
| 32 |
+
iface = gr.Interface(
|
| 33 |
+
fn=generate_transcript,
|
| 34 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter video transcript here..."),
|
| 35 |
+
outputs=gr.Textbox(),
|
| 36 |
+
title="Transcript Generator",
|
| 37 |
+
description="Enter a video transcript, and this app will generate a new, similar transcript using GPT-2."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Launch the app
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
iface.launch()
|