varshap03 commited on
Commit
33d68f5
·
verified ·
1 Parent(s): 96ac8c5

Delete nlp2_0.py

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