Update app.py
Browse files
app.py
CHANGED
|
@@ -9,52 +9,47 @@ print("🔄 Loading Model...")
|
|
| 9 |
try:
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
| 12 |
-
print("✅ Model Loaded
|
| 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=
|
| 32 |
min_length=10,
|
| 33 |
-
num_beams=1,
|
| 34 |
-
|
| 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 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
title="Generative AI Project",
|
| 55 |
-
description="Fine-tuned Pegasus Model
|
| 56 |
cache_examples=False
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
-
interface.
|
|
|
|
| 9 |
try:
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
|
| 12 |
+
print("✅ Model Loaded!")
|
| 13 |
except Exception as e:
|
| 14 |
print(f"❌ Error loading model: {e}")
|
| 15 |
|
|
|
|
| 16 |
def generate_text(prompt):
|
| 17 |
print(f"📩 Input received: {prompt}")
|
| 18 |
+
|
|
|
|
| 19 |
if len(prompt.split()) < 3:
|
| 20 |
return "⚠️ text is too short, please write a full sentence."
|
| 21 |
+
|
| 22 |
try:
|
| 23 |
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
| 24 |
|
|
|
|
|
|
|
| 25 |
summary_ids = model.generate(
|
| 26 |
inputs["input_ids"],
|
| 27 |
+
max_length=120,
|
| 28 |
min_length=10,
|
| 29 |
+
num_beams=1,
|
| 30 |
+
early_stopping=True
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
decoded = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 34 |
cleaned_text = decoded.replace("<n>", " ").replace(" .", ".").strip()
|
| 35 |
|
|
|
|
| 36 |
return cleaned_text
|
| 37 |
|
| 38 |
except Exception as e:
|
|
|
|
| 39 |
return f"Error: {str(e)}"
|
| 40 |
|
|
|
|
| 41 |
interface = gr.Interface(
|
| 42 |
fn=generate_text,
|
| 43 |
+
|
| 44 |
+
inputs=gr.Textbox(lines=5, label="Input Text", placeholder="Write your topic here..."),
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
outputs=gr.Textbox(lines=10, label="Generated Content"),
|
| 48 |
+
|
| 49 |
title="Generative AI Project",
|
| 50 |
+
description="Fine-tuned Pegasus Model.",
|
| 51 |
cache_examples=False
|
| 52 |
)
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
+
interface.launch()
|