| import gradio as gr |
| import json |
| from docx import Document |
| import io |
|
|
| |
| def minify_json(input_text, uploaded_file): |
| try: |
| if uploaded_file is not None: |
| input_text = uploaded_file["data"].decode("utf-8") |
|
|
| json_content = json.loads(input_text) |
| minified_json = json.dumps(json_content, separators=(',', ':')) |
| return minified_json |
| except json.JSONDecodeError: |
| return "Invalid JSON input. Please provide a valid JSON." |
|
|
| |
| minify = gr.Interface( |
| minify_json, |
| [gr.Textbox(label="Input JSON Text"), gr.File(label="Upload JSON File")], |
| "text" |
| ) |
|
|
| |
| def minify_json_to_row(input_text, uploaded_file): |
| try: |
| if uploaded_file is not None: |
| input_text = uploaded_file["data"].decode("utf-8") |
|
|
| json_content = json.loads(input_text) |
| minified_rows = ["{"] |
| for key, value in json_content.items(): |
| serialized_value = json.dumps(value, separators=(',', ':')) |
| minified_rows.append(f' "{key}": {serialized_value}') |
| minified_rows.append("}") |
| return "\n".join(minified_rows) |
| except json.JSONDecodeError: |
| return "Invalid JSON input. Please provide a valid JSON." |
|
|
| |
| minify_to_row = gr.Interface( |
| minify_json_to_row, |
| [gr.Textbox(label="Input JSON Text"), gr.File(label="Upload JSON File")], |
| "text" |
| ) |
|
|
| |
| def check_input(input_text, uploaded_file): |
| if uploaded_file is not None: |
| input_text = uploaded_file["data"].decode("utf-8") |
| |
| if not input_text.strip(): |
| return "No words in the input." |
| words = input_text.split() |
| response = [f"'{word}' is a valid word." if word.isalpha() and ' ' not in word else f"'{word}' is not a valid word." for word in words] |
| return " ".join(response) |
|
|
| |
| should_we_translate = gr.Interface( |
| check_input, |
| [gr.Textbox(label="Input Text"), gr.File(label="Upload File")], |
| "text" |
| ) |
|
|
| |
| def read_word_document(uploaded_file): |
| if uploaded_file is not None: |
| |
| file_stream = io.BytesIO(uploaded_file[1]) |
| document = Document(file_stream) |
| text = '\n'.join([para.text for para in document.paragraphs]) |
| return text |
| return "Please upload a Word document." |
|
|
|
|
|
|
| |
| read_docx = gr.Interface( |
| read_word_document, |
| gr.File(label="Upload Word Document"), |
| "text" |
| ) |
|
|
| |
| demo = gr.TabbedInterface([minify, should_we_translate, minify_to_row, read_docx], ["Minify JSON", "Should we translate?", "Minify to row", "Template"]) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|