Upload app.py.py
Browse files
app.py.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""nlp2.0.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1ybbW8OhudgBtSnghE8o6Z4tREzPlUx7S
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install transformers gradio accelerate
|
| 11 |
+
|
| 12 |
+
from transformers import pipeline
|
| 13 |
+
|
| 14 |
+
# Load the FLAN Alpaca Large model
|
| 15 |
+
paraphraser = pipeline("text2text-generation", model="declare-lab/flan-alpaca-large")
|
| 16 |
+
|
| 17 |
+
!pip install transformers gradio sentencepiece
|
| 18 |
+
|
| 19 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 20 |
+
import gradio as gr
|
| 21 |
+
|
| 22 |
+
# Load model and tokenizer
|
| 23 |
+
model_name = "ramsrigouthamg/t5_paraphraser"
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 25 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 26 |
+
|
| 27 |
+
# Define style prompts
|
| 28 |
+
def generate_prompt(text, style):
|
| 29 |
+
style_prompts = {
|
| 30 |
+
"Formal": "Please rewrite the following text in a formal and professional tone:\n\n",
|
| 31 |
+
"Friendly": "Please rewrite the following text in a casual and friendly tone:\n\n",
|
| 32 |
+
"Poetic": "Rewrite the text in a poetic and metaphorical way, like a short verse:\n\n",
|
| 33 |
+
"Gen Z": "Rewrite this text using Gen Z slang, internet expressions, abbreviations, and emojis:\n\n"
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Default if style not found
|
| 37 |
+
base_prompt = style_prompts.get(style, "Rewrite the text:\n\n")
|
| 38 |
+
return base_prompt + text
|
| 39 |
+
|
| 40 |
+
# Function to rewrite text in selected style
|
| 41 |
+
def rewrite_text(text, style):
|
| 42 |
+
if not text.strip():
|
| 43 |
+
return "Please enter some text."
|
| 44 |
+
|
| 45 |
+
# Build the prompt based on the selected style
|
| 46 |
+
if style == "Gen Z":
|
| 47 |
+
prompt = f"Rewrite the following text in a funny Gen Z tone with slang, emojis, and internet expressions:\n\n{text}"
|
| 48 |
+
elif style == "Poetic":
|
| 49 |
+
prompt = f"Rewrite the following text in a poetic and artistic style:\n\n{text}"
|
| 50 |
+
elif style == "Formal":
|
| 51 |
+
prompt = f"Rewrite the following text in a formal, professional tone:\n\n{text}"
|
| 52 |
+
elif style == "Friendly":
|
| 53 |
+
prompt = f"Rewrite the following text in a friendly and conversational style:\n\n{text}"
|
| 54 |
+
else:
|
| 55 |
+
prompt = f"Rewrite the following text:\n\n{text}"
|
| 56 |
+
|
| 57 |
+
# Call the model (paraphraser)
|
| 58 |
+
response = paraphraser(prompt, max_length=100)[0]['generated_text']
|
| 59 |
+
|
| 60 |
+
# Optional: clean output (remove repeated prompt from response if needed)
|
| 61 |
+
return response.replace(prompt, "").strip()
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Gradio UI
|
| 65 |
+
import gradio as gr
|
| 66 |
+
|
| 67 |
+
# Define the interface
|
| 68 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 69 |
+
gr.Markdown(
|
| 70 |
+
"""
|
| 71 |
+
<h1 style="text-align: center;">📝 Rewrite My Text</h1>
|
| 72 |
+
<p style="text-align: center;">Transform your text into <b>fun, formal, poetic, or Gen Z</b> styles using AI! 🚀</p>
|
| 73 |
+
""",
|
| 74 |
+
elem_id="header",
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
with gr.Column(scale=1):
|
| 79 |
+
input_text = gr.Textbox(
|
| 80 |
+
label="Input Text",
|
| 81 |
+
placeholder="Enter your sentence here...",
|
| 82 |
+
lines=4
|
| 83 |
+
)
|
| 84 |
+
style = gr.Dropdown(
|
| 85 |
+
label="Choose Style",
|
| 86 |
+
choices=["Gen Z", "Formal", "Poetic", "Friendly"],
|
| 87 |
+
value="Gen Z"
|
| 88 |
+
)
|
| 89 |
+
submit_button = gr.Button("✨ Submit", variant="primary")
|
| 90 |
+
clear_button = gr.Button("🧹 Clear")
|
| 91 |
+
|
| 92 |
+
with gr.Column(scale=1):
|
| 93 |
+
output_text = gr.Textbox(
|
| 94 |
+
label="Rewritten Text",
|
| 95 |
+
placeholder="Your rewritten sentence will appear here...",
|
| 96 |
+
lines=4
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Button functionality
|
| 100 |
+
submit_button.click(fn=rewrite_text, inputs=[input_text, style], outputs=output_text)
|
| 101 |
+
clear_button.click(fn=lambda: ("", ""), inputs=[], outputs=[input_text, output_text])
|
| 102 |
+
|
| 103 |
+
# Launch the app
|
| 104 |
+
demo.launch(share=True)
|