File size: 3,029 Bytes
171689f 98483eb 2952498 171689f b7e8935 98483eb 0f7c526 b7e8935 98483eb b7e8935 171689f 2952498 98483eb b7e8935 2d1bed7 171689f 0235378 fb490b2 0235378 57f9119 0235378 fb490b2 2952498 fb490b2 2952498 b7e8935 0f7c526 b7e8935 2952498 b7e8935 98483eb 2952498 6e91837 2952498 8a4580b 6e91837 2952498 fb490b2 2952498 7742e91 4147187 c78d2b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
import gradio as gr
import json
from docx import Document
import io
# Function to minify JSON
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."
# Gradio interface for minifying JSON
minify = gr.Interface(
minify_json,
[gr.Textbox(label="Input JSON Text"), gr.File(label="Upload JSON File")],
"text"
)
# Function to minify JSON with each key-value pair on a new line without extra indentation
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."
# Gradio interface for minifying JSON to rows
minify_to_row = gr.Interface(
minify_json_to_row,
[gr.Textbox(label="Input JSON Text"), gr.File(label="Upload JSON File")],
"text"
)
# Function for the 'Should we translate?' interface
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)
# Gradio interface for checking if we should translate
should_we_translate = gr.Interface(
check_input,
[gr.Textbox(label="Input Text"), gr.File(label="Upload File")],
"text"
)
# Function to read text from a Word document
def read_word_document(uploaded_file):
if uploaded_file is not None:
# The file content is in uploaded_file[1], which is bytes
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."
# Gradio interface for reading Word document
read_docx = gr.Interface(
read_word_document,
gr.File(label="Upload Word Document"),
"text"
)
# Creating a Tabbed Interface with all functionalities
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()
|