Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,78 @@
|
|
| 1 |
-
# import torch
|
| 2 |
-
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 3 |
-
# print(f"Using device: {device}")
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
model_name = "rajistics/informal_formal_style_transfer"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 10 |
-
import pandas as pd
|
| 11 |
|
|
|
|
| 12 |
df = pd.read_csv("hf://datasets/thesherrycode/gen-z-slangs-translation/gen_z_slangs_translation.csv")
|
| 13 |
-
|
| 14 |
-
# Rename the actual columns
|
| 15 |
df = df[["Gen-Z Slang", "Plain English"]].dropna().drop_duplicates()
|
| 16 |
df.columns = ["slang", "formal"]
|
| 17 |
-
df.head()
|
| 18 |
|
|
|
|
| 19 |
gradio_examples = [["[Gen-Z Example] " + row["slang"]] for _, row in df.sample(3, random_state=1).iterrows()]
|
| 20 |
-
gradio_examples
|
| 21 |
|
|
|
|
| 22 |
examples = [
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
]
|
| 29 |
|
| 30 |
-
# Clean
|
| 31 |
def clean_output(output: str):
|
| 32 |
-
|
| 33 |
|
| 34 |
-
#
|
| 35 |
def make_formal(text):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
|
|
|
| 44 |
iface = gr.Interface(
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
import torch
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import re
|
| 5 |
+
import gradio as gr
|
| 6 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 7 |
|
| 8 |
+
# Set device
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
print(f"Using device: {device}")
|
| 11 |
+
|
| 12 |
+
# Load model
|
| 13 |
model_name = "rajistics/informal_formal_style_transfer"
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 15 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)
|
|
|
|
| 16 |
|
| 17 |
+
# Load dataset
|
| 18 |
df = pd.read_csv("hf://datasets/thesherrycode/gen-z-slangs-translation/gen_z_slangs_translation.csv")
|
|
|
|
|
|
|
| 19 |
df = df[["Gen-Z Slang", "Plain English"]].dropna().drop_duplicates()
|
| 20 |
df.columns = ["slang", "formal"]
|
|
|
|
| 21 |
|
| 22 |
+
# Random samples from dataset
|
| 23 |
gradio_examples = [["[Gen-Z Example] " + row["slang"]] for _, row in df.sample(3, random_state=1).iterrows()]
|
|
|
|
| 24 |
|
| 25 |
+
# Manual examples
|
| 26 |
examples = [
|
| 27 |
+
["hey, can u send me the stuff by tonight?"],
|
| 28 |
+
["yo sorry i missed the call, was busy"],
|
| 29 |
+
["lemme know if ur free tmrw to chat abt the thing"],
|
| 30 |
+
["bro the file’s messed up, fix it asap pls"],
|
| 31 |
+
["i'm out rn, text u later"]
|
| 32 |
]
|
| 33 |
|
| 34 |
+
# Clean output
|
| 35 |
def clean_output(output: str):
|
| 36 |
+
return re.sub(r"(?i)make this sentence more formal", "", output).strip()
|
| 37 |
|
| 38 |
+
# Model inference
|
| 39 |
def make_formal(text):
|
| 40 |
+
if not text.strip():
|
| 41 |
+
return "⚠️ Please enter some text."
|
| 42 |
+
prompt = "[Casual] " + text.strip() + " [Formal]"
|
| 43 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True).to(device)
|
| 44 |
+
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 45 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 46 |
+
return clean_output(result)
|
| 47 |
|
| 48 |
+
# Gradio app
|
| 49 |
iface = gr.Interface(
|
| 50 |
+
fn=make_formal,
|
| 51 |
+
inputs=gr.Textbox(
|
| 52 |
+
lines=3,
|
| 53 |
+
label="🗣️ Your Slang / Casual Text",
|
| 54 |
+
placeholder="e.g., yo can u help me out real quick?"
|
| 55 |
+
),
|
| 56 |
+
outputs=gr.Textbox(
|
| 57 |
+
label="📄 Formal & Polished Version",
|
| 58 |
+
lines=4,
|
| 59 |
+
interactive=True
|
| 60 |
+
),
|
| 61 |
+
title="💬 Text Polisher: From Slang to Formal",
|
| 62 |
+
description=(
|
| 63 |
+
"Transform casual, Gen-Z slang, or unpolished English into clear, professional language. 🧠✨\n\n"
|
| 64 |
+
"This demo uses a text generation model to rewrite input sentences with improved formality — great for school, work, or writing more professionally.\n\n"
|
| 65 |
+
"✍️ The output is editable — feel free to tweak before using/copying!"
|
| 66 |
+
),
|
| 67 |
+
article=(
|
| 68 |
+
"**Project by Jonathan Friedman** \n"
|
| 69 |
+
"📌 Task: Text Generation (Formality Transfer using Sequence-to-Sequence)\n"
|
| 70 |
+
"🧠 Model: rajistics/informal_formal_style_transfer\n"
|
| 71 |
+
"📚 Dataset: thesherrycode/gen-z-slangs-translation\n"
|
| 72 |
+
"🛠️ Tech Stack: Hugging Face Transformers, Gradio"
|
| 73 |
+
),
|
| 74 |
+
examples=examples + gradio_examples,
|
| 75 |
+
theme="soft"
|
| 76 |
)
|
| 77 |
|
| 78 |
+
iface.launch()
|
|
|