Update app.py
Browse files
app.py
CHANGED
|
@@ -2,49 +2,59 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import torch
|
| 4 |
|
|
|
|
| 5 |
model_id = "GannaEslam38/Pegasus-Arxiv-Generator"
|
| 6 |
|
| 7 |
print("π Loading Model...")
|
| 8 |
try:
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
| 11 |
-
print("β
Model Loaded!")
|
| 12 |
except Exception as e:
|
| 13 |
print(f"β Error loading model: {e}")
|
| 14 |
|
|
|
|
| 15 |
def generate_text(prompt):
|
| 16 |
print(f"π© Input received: {prompt}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
| 19 |
|
|
|
|
|
|
|
| 20 |
summary_ids = model.generate(
|
| 21 |
inputs["input_ids"],
|
| 22 |
-
max_length=
|
| 23 |
min_length=10,
|
| 24 |
-
num_beams=1,
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
decoded = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 29 |
cleaned_text = decoded.replace("<n>", " ").replace(" .", ".").strip()
|
| 30 |
|
|
|
|
| 31 |
return cleaned_text
|
| 32 |
|
| 33 |
except Exception as e:
|
|
|
|
| 34 |
return f"Error: {str(e)}"
|
| 35 |
|
|
|
|
| 36 |
interface = gr.Interface(
|
| 37 |
fn=generate_text,
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
outputs=gr.Textbox(lines=10, label="Generated Content"),
|
| 42 |
-
|
| 43 |
title="Generative AI Project",
|
| 44 |
-
description="Fine-tuned Pegasus Model.",
|
| 45 |
-
examples=[["Artificial intelligence is transforming the world"]],
|
| 46 |
cache_examples=False
|
| 47 |
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
-
interface.launch()
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
|
| 6 |
model_id = "GannaEslam38/Pegasus-Arxiv-Generator"
|
| 7 |
|
| 8 |
print("π Loading Model...")
|
| 9 |
try:
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
| 12 |
+
print("β
Model Loaded Successfully!")
|
| 13 |
except Exception as e:
|
| 14 |
print(f"β Error loading model: {e}")
|
| 15 |
|
| 16 |
+
|
| 17 |
def generate_text(prompt):
|
| 18 |
print(f"π© Input received: {prompt}")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
if len(prompt.split()) < 3:
|
| 22 |
+
return "β οΈ text is too short, please write a full sentence."
|
| 23 |
+
|
| 24 |
try:
|
| 25 |
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
| 26 |
|
| 27 |
+
print("π§ Generating...")
|
| 28 |
+
|
| 29 |
summary_ids = model.generate(
|
| 30 |
inputs["input_ids"],
|
| 31 |
+
max_length=100,
|
| 32 |
min_length=10,
|
| 33 |
+
num_beams=1,
|
| 34 |
+
do_sample=False,
|
| 35 |
+
no_repeat_ngram_size=2
|
| 36 |
+
|
| 37 |
)
|
| 38 |
|
| 39 |
decoded = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 40 |
cleaned_text = decoded.replace("<n>", " ").replace(" .", ".").strip()
|
| 41 |
|
| 42 |
+
print(f"β
Output: {cleaned_text}")
|
| 43 |
return cleaned_text
|
| 44 |
|
| 45 |
except Exception as e:
|
| 46 |
+
print(f"β Error: {e}")
|
| 47 |
return f"Error: {str(e)}"
|
| 48 |
|
| 49 |
+
|
| 50 |
interface = gr.Interface(
|
| 51 |
fn=generate_text,
|
| 52 |
+
inputs=gr.Textbox(lines=5, label="Input Text", placeholder="Deep learning allows computers to..."),
|
| 53 |
+
outputs=gr.Textbox(lines=10, label="Generated Content"),
|
|
|
|
|
|
|
|
|
|
| 54 |
title="Generative AI Project",
|
| 55 |
+
description="Fine-tuned Pegasus Model on ArXiv Papers.",
|
|
|
|
| 56 |
cache_examples=False
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
interface.queue().launch()
|