Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# Load the tokenizer and model from the downloaded directory
|
| 5 |
+
model_name_or_path = 'model_directory'
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)
|
| 8 |
+
|
| 9 |
+
# Define the inference function
|
| 10 |
+
def generate_summary(text):
|
| 11 |
+
inputs = tokenizer.encode("summarize: " + text, return_tensors="pt", max_length=512, truncation=True)
|
| 12 |
+
summary_ids = model.generate(inputs, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)
|
| 13 |
+
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 14 |
+
|
| 15 |
+
# Define the Gradio interface
|
| 16 |
+
def inference(text):
|
| 17 |
+
summary = generate_summary(text)
|
| 18 |
+
return summary
|
| 19 |
+
|
| 20 |
+
interface = gr.Interface(fn=inference, inputs="text", outputs="text", title="Text Summarization", description="Enter text to summarize")
|
| 21 |
+
|
| 22 |
+
# Launch the Gradio interface
|
| 23 |
+
interface.launch()
|