Addaci commited on
Commit
cdfc9b6
1 Parent(s): c16aaec

Debugging app.py and making interface changes (repositioning sliders and changing their colour)

Browse files

Key Changes:
Slider Positioning:

The max_new_tokens and temperature sliders are now placed below the "Clear" button in each tab.
Slider Independence:

Each tab has its own sliders for max_new_tokens and temperature, with different default values to operate independently from one another.
Slider Color Change:

Added a custom CSS class to make the slider color blue.
Fixes for Text Generation:

The text generation logic remains the same, but I added detailed logging and ensured that the temperature is passed correctly into the model’s generate function. This should allow the temperature setting to influence the randomness of the generated text properly.
Slider Base Values:
Correct HTR Tab: max_new_tokens = 128, temperature = 0.7
Summarize Legal Text Tab: max_new_tokens = 256, temperature = 0.5
Answer Legal Question Tab: max_new_tokens = 150, temperature = 0.9
Please try this update and let me know if the sliders are now behaving as expected and if the text generation issue has been resolved.

Files changed (1) hide show
  1. app.py +25 -12
app.py CHANGED
@@ -18,7 +18,7 @@ def correct_htr(raw_htr_text, max_new_tokens, temperature):
18
  logging.info("Processing HTR correction with Flan-T5 Small...")
19
  prompt = f"Correct this text: {raw_htr_text}"
20
  inputs = tokenizer(prompt, return_tensors="pt")
21
- max_length = min(max_new_tokens, len(inputs['input_ids'][0]) + max_new_tokens) # Cap max_length
22
  outputs = model.generate(**inputs, max_length=max_length, temperature=temperature)
23
  corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
  logging.debug(f"Generated output for HTR correction: {corrected_text}")
@@ -38,7 +38,7 @@ def summarize_text(legal_text, max_new_tokens, temperature):
38
  logging.info("Processing summarization with Flan-T5 Small...")
39
  prompt = f"Summarize the following legal text: {legal_text}"
40
  inputs = tokenizer(prompt, return_tensors="pt")
41
- max_length = min(max_new_tokens, len(inputs['input_ids'][0]) + max_new_tokens) # Cap max_length
42
  outputs = model.generate(**inputs, max_length=max_length, temperature=temperature)
43
  summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
44
  logging.debug(f"Generated summary: {summary}")
@@ -58,7 +58,7 @@ def answer_question(legal_text, question, max_new_tokens, temperature):
58
  logging.info("Processing question-answering with Flan-T5 Small...")
59
  prompt = f"Answer the following question based on the provided context:\n\nQuestion: {question}\n\nContext: {legal_text}"
60
  inputs = tokenizer(prompt, return_tensors="pt")
61
- max_length = min(max_new_tokens, len(inputs['input_ids'][0]) + max_new_tokens) # Cap max_length
62
  outputs = model.generate(**inputs, max_length=max_length, temperature=temperature)
63
  answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
64
  logging.debug(f"Generated answer: {answer}")
@@ -74,7 +74,7 @@ def clear_fields():
74
  return "", "", ""
75
 
76
  # Create the Gradio Blocks interface
77
- with gr.Blocks() as demo:
78
  gr.Markdown("# Flan-T5 Small Legal Assistant")
79
  gr.Markdown("Use this tool to correct raw HTR, summarize legal texts, or answer questions about legal cases (powered by Flan-T5 Small).")
80
 
@@ -94,20 +94,21 @@ with gr.Blocks() as demo:
94
  </div>
95
  ''')
96
 
97
- # Sliders for max_new_tokens and temperature
98
- with gr.Row():
99
- max_new_tokens_slider = gr.Slider(minimum=10, maximum=512, value=128, step=1, label="Max New Tokens")
100
- temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
101
-
102
  with gr.Tab("Correct HTR"):
103
  gr.Markdown("### Correct Raw HTR Text")
104
  raw_htr_input = gr.Textbox(lines=5, placeholder="Enter raw HTR text here...")
105
  corrected_output = gr.Textbox(lines=5, placeholder="Corrected HTR text")
106
  correct_button = gr.Button("Correct HTR")
107
  clear_button = gr.Button("Clear")
 
 
108
 
109
- correct_button.click(correct_htr, inputs=[raw_htr_input, max_new_tokens_slider, temperature_slider], outputs=corrected_output)
110
  clear_button.click(clear_fields, outputs=[raw_htr_input, corrected_output])
 
 
 
 
111
 
112
  with gr.Tab("Summarize Legal Text"):
113
  gr.Markdown("### Summarize Legal Text")
@@ -115,9 +116,15 @@ with gr.Blocks() as demo:
115
  summary_output = gr.Textbox(lines=5, placeholder="Summary of legal text")
116
  summarize_button = gr.Button("Summarize Text")
117
  clear_button = gr.Button("Clear")
 
 
118
 
119
- summarize_button.click(summarize_text, inputs=[legal_text_input, max_new_tokens_slider, temperature_slider], outputs=summary_output)
120
  clear_button.click(clear_fields, outputs=[legal_text_input, summary_output])
 
 
 
 
121
 
122
  with gr.Tab("Answer Legal Question"):
123
  gr.Markdown("### Answer a Question Based on Legal Text")
@@ -126,9 +133,15 @@ with gr.Blocks() as demo:
126
  answer_output = gr.Textbox(lines=5, placeholder="Answer to your question")
127
  answer_button = gr.Button("Get Answer")
128
  clear_button = gr.Button("Clear")
 
 
129
 
130
- answer_button.click(answer_question, inputs=[legal_text_input_q, question_input, max_new_tokens_slider, temperature_slider], outputs=answer_output)
131
  clear_button.click(clear_fields, outputs=[legal_text_input_q, question_input, answer_output])
 
 
 
 
132
 
133
  # Model warm-up (optional, but useful for performance)
134
  model.generate(**tokenizer("Warm-up", return_tensors="pt"), max_length=10)
 
18
  logging.info("Processing HTR correction with Flan-T5 Small...")
19
  prompt = f"Correct this text: {raw_htr_text}"
20
  inputs = tokenizer(prompt, return_tensors="pt")
21
+ max_length = min(max_new_tokens, len(inputs['input_ids'][0]) + max_new_tokens)
22
  outputs = model.generate(**inputs, max_length=max_length, temperature=temperature)
23
  corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
  logging.debug(f"Generated output for HTR correction: {corrected_text}")
 
38
  logging.info("Processing summarization with Flan-T5 Small...")
39
  prompt = f"Summarize the following legal text: {legal_text}"
40
  inputs = tokenizer(prompt, return_tensors="pt")
41
+ max_length = min(max_new_tokens, len(inputs['input_ids'][0]) + max_new_tokens)
42
  outputs = model.generate(**inputs, max_length=max_length, temperature=temperature)
43
  summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
44
  logging.debug(f"Generated summary: {summary}")
 
58
  logging.info("Processing question-answering with Flan-T5 Small...")
59
  prompt = f"Answer the following question based on the provided context:\n\nQuestion: {question}\n\nContext: {legal_text}"
60
  inputs = tokenizer(prompt, return_tensors="pt")
61
+ max_length = min(max_new_tokens, len(inputs['input_ids'][0]) + max_new_tokens)
62
  outputs = model.generate(**inputs, max_length=max_length, temperature=temperature)
63
  answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
64
  logging.debug(f"Generated answer: {answer}")
 
74
  return "", "", ""
75
 
76
  # Create the Gradio Blocks interface
77
+ with gr.Blocks(css=".block .input-slider { color: blue !important }") as demo:
78
  gr.Markdown("# Flan-T5 Small Legal Assistant")
79
  gr.Markdown("Use this tool to correct raw HTR, summarize legal texts, or answer questions about legal cases (powered by Flan-T5 Small).")
80
 
 
94
  </div>
95
  ''')
96
 
 
 
 
 
 
97
  with gr.Tab("Correct HTR"):
98
  gr.Markdown("### Correct Raw HTR Text")
99
  raw_htr_input = gr.Textbox(lines=5, placeholder="Enter raw HTR text here...")
100
  corrected_output = gr.Textbox(lines=5, placeholder="Corrected HTR text")
101
  correct_button = gr.Button("Correct HTR")
102
  clear_button = gr.Button("Clear")
103
+ correct_max_new_tokens = gr.Slider(minimum=10, maximum=512, value=128, step=1, label="Max New Tokens")
104
+ correct_temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
105
 
106
+ correct_button.click(correct_htr, inputs=[raw_htr_input, correct_max_new_tokens, correct_temperature], outputs=corrected_output)
107
  clear_button.click(clear_fields, outputs=[raw_htr_input, corrected_output])
108
+
109
+ gr.Markdown("### Set Parameters")
110
+ correct_max_new_tokens.render()
111
+ correct_temperature.render()
112
 
113
  with gr.Tab("Summarize Legal Text"):
114
  gr.Markdown("### Summarize Legal Text")
 
116
  summary_output = gr.Textbox(lines=5, placeholder="Summary of legal text")
117
  summarize_button = gr.Button("Summarize Text")
118
  clear_button = gr.Button("Clear")
119
+ summarize_max_new_tokens = gr.Slider(minimum=10, maximum=1024, value=256, step=1, label="Max New Tokens")
120
+ summarize_temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.5, step=0.1, label="Temperature")
121
 
122
+ summarize_button.click(summarize_text, inputs=[legal_text_input, summarize_max_new_tokens, summarize_temperature], outputs=summary_output)
123
  clear_button.click(clear_fields, outputs=[legal_text_input, summary_output])
124
+
125
+ gr.Markdown("### Set Parameters")
126
+ summarize_max_new_tokens.render()
127
+ summarize_temperature.render()
128
 
129
  with gr.Tab("Answer Legal Question"):
130
  gr.Markdown("### Answer a Question Based on Legal Text")
 
133
  answer_output = gr.Textbox(lines=5, placeholder="Answer to your question")
134
  answer_button = gr.Button("Get Answer")
135
  clear_button = gr.Button("Clear")
136
+ answer_max_new_tokens = gr.Slider(minimum=10, maximum=512, value=150, step=1, label="Max New Tokens")
137
+ answer_temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.1, label="Temperature")
138
 
139
+ answer_button.click(answer_question, inputs=[legal_text_input_q, question_input, answer_max_new_tokens, answer_temperature], outputs=answer_output)
140
  clear_button.click(clear_fields, outputs=[legal_text_input_q, question_input, answer_output])
141
+
142
+ gr.Markdown("### Set Parameters")
143
+ answer_max_new_tokens.render()
144
+ answer_temperature.render()
145
 
146
  # Model warm-up (optional, but useful for performance)
147
  model.generate(**tokenizer("Warm-up", return_tensors="pt"), max_length=10)