Spaces:
Runtime error
Runtime error
| import spaces | |
| import gradio as gr | |
| import torch | |
| from transformers import AutoTokenizer | |
| tokenizer = AutoTokenizer.from_pretrained("gpt2") | |
| def temp(text): | |
| response = tokenizer(input_text, return_tensors="pt") | |
| return response | |
| def greet(input_text, length, function_words, grade_level, sarcasm, formality, voice, persuasive, descriptive, narrative, expository): | |
| response = ( | |
| f"Hello!\n" | |
| f"Input Text: {input_text}\n" | |
| f"Length: {length}\n" | |
| f"Function Words: {function_words}\n" | |
| f"Grade Level: {grade_level}\n" | |
| f"Sarcasm: {sarcasm}\n" | |
| f"Formality: {formality}\n" | |
| f"Voice: {voice}\n" | |
| f"Persuasive: {persuasive}\n" | |
| f"Descriptive: {descriptive}\n" | |
| f"Narrative: {narrative}\n" | |
| f"Expository: {expository}" | |
| ) | |
| return response | |
| def reset_sliders(): | |
| return [0.5] * 10 | |
| def toggle_slider(checked, value): | |
| if checked: | |
| return gr.update(value=value, interactive=True) | |
| else: | |
| return gr.update(value=0, interactive=False) | |
| def reset_writing_type_sliders(selected_type): | |
| reset_values = [gr.update(value=0, interactive=False) for _ in range(4)] | |
| if selected_type != "None": | |
| index = ["Persuasive", "Descriptive", "Narrative", "Expository"].index(selected_type) | |
| reset_values[index] = gr.update(value=0, interactive=True) | |
| return reset_values | |
| demo = gr.Blocks() | |
| with demo: | |
| with gr.Row(): | |
| with gr.Column(variant="panel"): | |
| gr.Markdown("# 1) Input Text\n### Enter the text to be obfuscated.") | |
| input_text = gr.Textbox( | |
| label="Input Text", | |
| value="The quick brown fox jumped over the lazy dogs." | |
| ) | |
| gr.Markdown("# 2) Style Element Sliders\n### Adjust the style element sliders to the desired levels to steer the obfuscation.") | |
| reset_button = gr.Button("Choose slider values automatically (based on input text)") | |
| sliders = [] | |
| slider_values = [ | |
| ("Length (Shorter \u2192 Longer)", -1, 1, 0), | |
| ("Function Words (Fewer \u2192 More)", -1, 1, 0), | |
| ("Grade Level (Lower \u2192 Higher)", -1, 1, 0), | |
| ("Formality (Less \u2192 More)", -1, 1, 0), | |
| ("Sarcasm (Less \u2192 More)", -1, 1, 0), | |
| ("Voice (Passive \u2192 Active)", -1, 1, 0), | |
| ("Writing Type: Persuasive (None \u2192 More)", 0, 1, 0), | |
| ("Writing Type: Descriptive (None \u2192 More)", 0, 1, 0), | |
| ("Writing Type: Narrative (None \u2192 More)", 0, 1, 0), | |
| ("Writing Type: Expository (None \u2192 More)", 0, 1, 0) | |
| ] | |
| non_writing_type_sliders = [] | |
| writing_type_sliders = [] | |
| for idx, (label, min_val, max_val, default) in enumerate(slider_values): | |
| with gr.Row(): | |
| if "Writing Type" not in label: | |
| checkbox = gr.Checkbox(label=label) | |
| slider = gr.Slider(label=label, minimum=min_val, maximum=max_val, step=0.01, value=default, interactive=False) | |
| checkbox.change(fn=toggle_slider, inputs=[checkbox, gr.State(default)], outputs=slider) | |
| non_writing_type_sliders.append(slider) | |
| else: | |
| slider = gr.Slider(label=label, minimum=min_val, maximum=max_val, step=0.01, value=default, interactive=False) | |
| writing_type_sliders.append(slider) | |
| sliders.append(slider) | |
| writing_type_radio = gr.Radio( | |
| label="Writing Type", | |
| choices=["None", "Persuasive", "Descriptive", "Narrative", "Expository"], | |
| value="None" | |
| ) | |
| obfuscate_button = gr.Button("Obfuscate Text") | |
| reset_button.click(fn=reset_sliders, inputs=[], outputs=sliders) | |
| writing_type_radio.change(fn=reset_writing_type_sliders, inputs=writing_type_radio, outputs=writing_type_sliders) | |
| with gr.Column(variant="panel"): | |
| output = gr.Textbox(label="Output") | |
| obfuscate_button.click(fn=greet, inputs=[input_text] + sliders, outputs=output) | |
| demo.launch() | |