janbanot commited on
Commit
993c87f
·
1 Parent(s): b1632ff

feat: formalize mode changes

Browse files
Files changed (1) hide show
  1. app.py +34 -16
app.py CHANGED
@@ -34,7 +34,8 @@ model = AutoModelForCausalLM.from_pretrained(
34
 
35
  @spaces.GPU
36
  def generate(
37
- prompt,
 
38
  temperature,
39
  max_tokens,
40
  top_k,
@@ -44,13 +45,16 @@ def generate(
44
  streamer = TextIteratorStreamer(
45
  tokenizer, skip_prompt=True, skip_special_tokens=True
46
  )
47
- system = "Jesteś chatbotem udzielającym odpowiedzi na pytania w języku polskim"
 
 
 
48
  messages = []
49
 
50
  if system:
51
  messages.append({"role": "system", "content": system})
52
 
53
- messages.append({"role": "user", "content": prompt})
54
 
55
  tokenizer_output = tokenizer.apply_chat_template(
56
  messages, return_tensors="pt", return_dict=True
@@ -88,6 +92,13 @@ def generate(
88
  yield cleaned_response
89
 
90
 
 
 
 
 
 
 
 
91
  with gr.Blocks(
92
  css="""
93
  .gradio-container { max-width: 1600px; margin: 20px; padding: 10px; }
@@ -100,8 +111,8 @@ with gr.Blocks(
100
 
101
  with gr.Column(elem_id="main-content"):
102
  with gr.Row():
103
- simple_question_btn = gr.Button("Proste Pytanie", variant="primary")
104
- formalizer_btn = gr.Button("Formalizator", variant="secondary")
105
  judge_btn = gr.Button("Sędzia", interactive=False)
106
 
107
  # Function to switch tool visibility and update button styles based on the active tool
@@ -128,7 +139,7 @@ with gr.Blocks(
128
  with gr.Row():
129
  generate_btn_sq = gr.Button("Generuj odpowiedź", interactive=False)
130
  clear_btn_sq = gr.Button("Wyczyść", interactive=False)
131
- output_text_sq = gr.Textbox(label="Odpowiedź", interactive=False)
132
 
133
  with gr.Accordion("⚙️ Parametry", open=False):
134
  temperature_sq = gr.Slider(0, 1, 0.3, step=0.1, label="Temperatura")
@@ -165,6 +176,7 @@ with gr.Blocks(
165
  fn=generate,
166
  inputs=[
167
  input_text_sq,
 
168
  temperature_sq,
169
  max_tokens_sq,
170
  top_k_sq,
@@ -186,7 +198,7 @@ with gr.Blocks(
186
  )
187
  with gr.Row():
188
  gr.Text(
189
- "Wybierz styl tekstu:",
190
  elem_id="style-label",
191
  show_label=False,
192
  elem_classes="same-height",
@@ -209,14 +221,7 @@ with gr.Blocks(
209
  elem_id="clear-btn",
210
  elem_classes="same-height",
211
  )
212
- output_text = gr.Textbox(label="Wynik", interactive=False)
213
-
214
- # Mock processing function to simulate text formalization with a delay
215
- def mock_processing_function(input_text):
216
- import time
217
-
218
- time.sleep(1) # Simulate processing delay
219
- return input_text + " [Formalized]"
220
 
221
  # Update button states based on input and output text changes for interactivity
222
  def update_button_states(input_text, output_text):
@@ -240,7 +245,20 @@ with gr.Blocks(
240
 
241
  # Event handlers for button actions to process and clear text
242
  generate_btn.click(
243
- fn=mock_processing_function, inputs=input_text, outputs=output_text
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  )
245
 
246
  clear_btn.click(
 
34
 
35
  @spaces.GPU
36
  def generate(
37
+ user_input,
38
+ prompt_style,
39
  temperature,
40
  max_tokens,
41
  top_k,
 
45
  streamer = TextIteratorStreamer(
46
  tokenizer, skip_prompt=True, skip_special_tokens=True
47
  )
48
+ system = f"""Jesteś pomocnym botem udzielającym odpowiedzi na pytania w języku polskim.
49
+ Odpowiadaj krótko i zwięźle, unikaj zbyt skomplikowanych odpowiedzi.
50
+ {prompt_style}
51
+ """
52
  messages = []
53
 
54
  if system:
55
  messages.append({"role": "system", "content": system})
56
 
57
+ messages.append({"role": "user", "content": user_input})
58
 
59
  tokenizer_output = tokenizer.apply_chat_template(
60
  messages, return_tensors="pt", return_dict=True
 
92
  yield cleaned_response
93
 
94
 
95
+ STYLE_PROMPTS = {
96
+ "Formalny": """Przekształć poniższy tekst na bardziej formalny, zachowując jego oryginalne znaczenie i klarowność.""", # noqa
97
+ "Nieformalny": """Przekształć poniższy tekst na luźniejszy i bardziej nieformalny, tak żeby brzmiał swobodnie i naturalnie..""", # noqa
98
+ "Neutralny": """Przekształć poniższy tekst na bardziej neutralny, eliminując zbyt formalne lub potoczne sformułowania.""", # noqa
99
+ }
100
+
101
+
102
  with gr.Blocks(
103
  css="""
104
  .gradio-container { max-width: 1600px; margin: 20px; padding: 10px; }
 
111
 
112
  with gr.Column(elem_id="main-content"):
113
  with gr.Row():
114
+ simple_question_btn = gr.Button("Zadaj Pytanie", variant="primary")
115
+ formalizer_btn = gr.Button("Zmiana stylu", variant="secondary")
116
  judge_btn = gr.Button("Sędzia", interactive=False)
117
 
118
  # Function to switch tool visibility and update button styles based on the active tool
 
139
  with gr.Row():
140
  generate_btn_sq = gr.Button("Generuj odpowiedź", interactive=False)
141
  clear_btn_sq = gr.Button("Wyczyść", interactive=False)
142
+ output_text_sq = gr.Textbox(label="Odpowiedź", interactive=False, lines=5)
143
 
144
  with gr.Accordion("⚙️ Parametry", open=False):
145
  temperature_sq = gr.Slider(0, 1, 0.3, step=0.1, label="Temperatura")
 
176
  fn=generate,
177
  inputs=[
178
  input_text_sq,
179
+ "",
180
  temperature_sq,
181
  max_tokens_sq,
182
  top_k_sq,
 
198
  )
199
  with gr.Row():
200
  gr.Text(
201
+ "Wybierz styl:",
202
  elem_id="style-label",
203
  show_label=False,
204
  elem_classes="same-height",
 
221
  elem_id="clear-btn",
222
  elem_classes="same-height",
223
  )
224
+ output_text = gr.Textbox(label="Wynik", interactive=False, lines=5)
 
 
 
 
 
 
 
225
 
226
  # Update button states based on input and output text changes for interactivity
227
  def update_button_states(input_text, output_text):
 
245
 
246
  # Event handlers for button actions to process and clear text
247
  generate_btn.click(
248
+ fn=lambda input_text, style: generate(
249
+ input_text,
250
+ STYLE_PROMPTS[style],
251
+ 0.3, # Hardcoded temperature
252
+ 1024, # Hardcoded max_tokens
253
+ 40, # Hardcoded top_k
254
+ 1.1, # Hardcoded repetition_penalty
255
+ 0.95 # Hardcoded top_p
256
+ ),
257
+ inputs=[
258
+ input_text,
259
+ style_dropdown
260
+ ],
261
+ outputs=output_text,
262
  )
263
 
264
  clear_btn.click(