varshap03 commited on
Commit
f79dad4
Β·
verified Β·
1 Parent(s): d84f325

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -22
app.py CHANGED
@@ -1,57 +1,89 @@
 
 
1
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
  import gradio as gr
3
- import torch
4
 
5
  # Load model and tokenizer
6
  model_name = "ramsrigouthamg/t5_paraphraser"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def rewrite_text(text, style):
11
  if not text.strip():
12
  return "Please enter some text."
13
 
14
- style_prompts = {
15
- "Formal": "Rewrite this formally: ",
16
- "Friendly": "Rewrite this casually and friendly: ",
17
- "Poetic": "Rewrite this like a poem: ",
18
- "Gen Z": "Rewrite this like a Gen Z person using slang and emojis: ",
19
- "Professional": "Rewrite this in a professional tone: ",
20
- "Witty": "Rewrite this in a witty and clever way: "
21
- }
 
 
 
22
 
23
- prompt = style_prompts.get(style, "Paraphrase: ") + text
 
24
 
25
- input_ids = tokenizer.encode(prompt, return_tensors="pt", max_length=256, truncation=True)
26
- output_ids = model.generate(input_ids, max_length=100, num_beams=5, early_stopping=True)
27
- output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
28
 
29
- return output
30
 
31
  # Gradio UI
 
 
 
32
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
33
  gr.Markdown(
34
  """
35
  <h1 style="text-align: center;">πŸ“ Rewrite My Text</h1>
36
- <p style="text-align: center;">Transform your text into <b>Formal, Friendly, Poetic, Gen Z, Witty</b> styles using AI! πŸš€</p>
37
- """
 
38
  )
39
 
40
  with gr.Row():
41
- with gr.Column():
42
- input_text = gr.Textbox(label="Input Text", placeholder="Enter your sentence...", lines=4)
 
 
 
 
43
  style = gr.Dropdown(
44
  label="Choose Style",
45
- choices=["Gen Z", "Formal", "Poetic", "Friendly", "Professional", "Witty"],
46
  value="Gen Z"
47
  )
48
  submit_button = gr.Button("✨ Submit", variant="primary")
49
  clear_button = gr.Button("🧹 Clear")
50
 
51
- with gr.Column():
52
- output_text = gr.Textbox(label="Rewritten Text", lines=4)
 
 
 
 
53
 
 
54
  submit_button.click(fn=rewrite_text, inputs=[input_text, style], outputs=output_text)
55
  clear_button.click(fn=lambda: ("", ""), inputs=[], outputs=[input_text, output_text])
56
 
57
- demo.launch()
 
 
 
1
+ !pip install transformers gradio sentencepiece
2
+
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
  import gradio as gr
 
5
 
6
  # Load model and tokenizer
7
  model_name = "ramsrigouthamg/t5_paraphraser"
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
10
 
11
+ # Define style prompts
12
+ def generate_prompt(text, style):
13
+ style_prompts = {
14
+ "Formal": "Please rewrite the following text in a formal and professional tone:\n\n",
15
+ "Friendly": "Please rewrite the following text in a casual and friendly tone:\n\n",
16
+ "Poetic": "Rewrite the text in a poetic and metaphorical way, like a short verse:\n\n",
17
+ "Gen Z": "Rewrite this text using Gen Z slang, internet expressions, abbreviations, and emojis:\n\n"
18
+ }
19
+
20
+ # Default if style not found
21
+ base_prompt = style_prompts.get(style, "Rewrite the text:\n\n")
22
+ return base_prompt + text
23
+
24
+ # Function to rewrite text in selected style
25
  def rewrite_text(text, style):
26
  if not text.strip():
27
  return "Please enter some text."
28
 
29
+ # Build the prompt based on the selected style
30
+ if style == "Gen Z":
31
+ prompt = f"Rewrite the following text in a funny Gen Z tone with slang, emojis, and internet expressions:\n\n{text}"
32
+ elif style == "Poetic":
33
+ prompt = f"Rewrite the following text in a poetic and artistic style:\n\n{text}"
34
+ elif style == "Formal":
35
+ prompt = f"Rewrite the following text in a formal, professional tone:\n\n{text}"
36
+ elif style == "Friendly":
37
+ prompt = f"Rewrite the following text in a friendly and conversational style:\n\n{text}"
38
+ else:
39
+ prompt = f"Rewrite the following text:\n\n{text}"
40
 
41
+ # Call the model (paraphraser)
42
+ response = paraphraser(prompt, max_length=100)[0]['generated_text']
43
 
44
+ # Optional: clean output (remove repeated prompt from response if needed)
45
+ return response.replace(prompt, "").strip()
 
46
 
 
47
 
48
  # Gradio UI
49
+ import gradio as gr
50
+
51
+ # Define the interface
52
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
53
  gr.Markdown(
54
  """
55
  <h1 style="text-align: center;">πŸ“ Rewrite My Text</h1>
56
+ <p style="text-align: center;">Transform your text into <b>fun, formal, poetic, or Gen Z</b> styles using AI! πŸš€</p>
57
+ """,
58
+ elem_id="header",
59
  )
60
 
61
  with gr.Row():
62
+ with gr.Column(scale=1):
63
+ input_text = gr.Textbox(
64
+ label="Input Text",
65
+ placeholder="Enter your sentence here...",
66
+ lines=4
67
+ )
68
  style = gr.Dropdown(
69
  label="Choose Style",
70
+ choices=["Gen Z", "Formal", "Poetic", "Friendly"],
71
  value="Gen Z"
72
  )
73
  submit_button = gr.Button("✨ Submit", variant="primary")
74
  clear_button = gr.Button("🧹 Clear")
75
 
76
+ with gr.Column(scale=1):
77
+ output_text = gr.Textbox(
78
+ label="Rewritten Text",
79
+ placeholder="Your rewritten sentence will appear here...",
80
+ lines=4
81
+ )
82
 
83
+ # Button functionality
84
  submit_button.click(fn=rewrite_text, inputs=[input_text, style], outputs=output_text)
85
  clear_button.click(fn=lambda: ("", ""), inputs=[], outputs=[input_text, output_text])
86
 
87
+ # Launch the app
88
+ demo.launch(share=True)
89
+