Tonic commited on
Commit
3988e91
1 Parent(s): 65bec20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -40
app.py CHANGED
@@ -56,7 +56,7 @@ def historical_generation(prompt, max_new_tokens=600):
56
  # Create highlighted text output
57
  highlighted_text = []
58
  for token in tokens:
59
- clean_token = token.replace("Ġ", "") # Remove "Ġ"
60
  token_type = tokenizer.convert_ids_to_tokens([tokenizer.convert_tokens_to_ids(token)])[0]
61
  highlighted_text.append((clean_token, token_type))
62
 
@@ -79,47 +79,47 @@ def text_analysis(text):
79
 
80
  return pos_tokens, pos_count, html
81
 
82
- # Full interface combining text generation and analysis
 
 
 
 
 
83
  def full_interface(prompt, max_new_tokens):
84
  generated_highlight, generated_text = historical_generation(prompt, max_new_tokens)
85
 
86
- # Dependency parse of both input and generated text
87
  tokens_input, pos_count_input, html_input = text_analysis(prompt)
88
- tokens_generated, pos_count_generated, html_generated = text_analysis(generated_text)
89
 
90
- return generated_highlight, pos_count_input, html_input, html_generated
91
-
92
- # Create Gradio interface
93
- iface = gr.Interface(
94
- fn=full_interface,
95
- inputs=[
96
- gr.Textbox(
97
- label="Prompt",
98
- placeholder="Enter a prompt for historical text generation...",
99
- lines=3
100
- ),
101
- gr.Slider(
102
- label="Max New Tokens",
103
- minimum=50,
104
- maximum=1000,
105
- step=50,
106
- value=600
107
- )
108
- ],
109
- outputs=[
110
- gr.HighlightedText(
111
- label="Generated Historical Text",
112
- combine_adjacent=True,
113
- show_legend=True
114
- ),
115
- gr.JSON(label="Tokenizer Info (Input Text)"),
116
- gr.HTML(label="Dependency Parse Visualization (Input Text)"),
117
- gr.HTML(label="Dependency Parse Visualization (Generated Text)")
118
- ],
119
- title="Historical Text Generation with OCRonos-Vintage",
120
- description="Generate historical-style text using OCRonos-Vintage and analyze the tokenizer output, including dependency parsing.",
121
- theme=gr.themes.Base()
122
- )
123
-
124
- if __name__ == "__main__":
125
- iface.launch()
 
56
  # Create highlighted text output
57
  highlighted_text = []
58
  for token in tokens:
59
+ clean_token = token.replace("Ġ", "")
60
  token_type = tokenizer.convert_ids_to_tokens([tokenizer.convert_tokens_to_ids(token)])[0]
61
  highlighted_text.append((clean_token, token_type))
62
 
 
79
 
80
  return pos_tokens, pos_count, html
81
 
82
+ # Function to generate dependency parse for generated text on button click
83
+ def generate_dependency_parse(generated_text):
84
+ tokens_generated, pos_count_generated, html_generated = text_analysis(generated_text)
85
+ return html_generated
86
+
87
+ # Full interface combining text generation and analysis, split across steps
88
  def full_interface(prompt, max_new_tokens):
89
  generated_highlight, generated_text = historical_generation(prompt, max_new_tokens)
90
 
91
+ # Dependency parse of input text
92
  tokens_input, pos_count_input, html_input = text_analysis(prompt)
 
93
 
94
+ # The "Send" button should now appear after these outputs are generated
95
+ return generated_highlight, pos_count_input, html_input, gr.update(visible=True), generated_text
96
+
97
+ # Gradio interface components
98
+ with gr.Blocks() as iface:
99
+ prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt for historical text generation...", lines=3)
100
+ max_new_tokens = gr.Slider(label="Max New Tokens", minimum=50, maximum=1000, step=50, value=600)
101
+
102
+ # Output components
103
+ highlighted_text = gr.HighlightedText(label="Generated Historical Text", combine_adjacent=True, show_legend=True)
104
+ tokenizer_info = gr.JSON(label="Tokenizer Info (Input Text)")
105
+ dependency_parse_input = gr.HTML(label="Dependency Parse Visualization (Input Text)")
106
+
107
+ # Hidden button and final output for dependency parse visualization
108
+ send_button = gr.Button(value="Generate Dependency Parse for Generated Text", visible=False)
109
+ dependency_parse_generated = gr.HTML(label="Dependency Parse Visualization (Generated Text)")
110
+
111
+ # Button behavior for generating final parse visualization
112
+ send_button.click(
113
+ generate_dependency_parse,
114
+ inputs=[dependency_parse_generated],
115
+ outputs=[dependency_parse_generated]
116
+ )
117
+
118
+ # Main interface logic
119
+ prompt.submit(
120
+ full_interface,
121
+ inputs=[prompt, max_new_tokens],
122
+ outputs=[highlighted_text, tokenizer_info, dependency_parse_input, send_button, dependency_parse_generated]
123
+ )
124
+
125
+ iface.launch()