Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Function to minify JSON
|
| 5 |
def minify_json(input_text, uploaded_file):
|
|
@@ -13,6 +15,7 @@ def minify_json(input_text, uploaded_file):
|
|
| 13 |
except json.JSONDecodeError:
|
| 14 |
return "Invalid JSON input. Please provide a valid JSON."
|
| 15 |
|
|
|
|
| 16 |
minify = gr.Interface(
|
| 17 |
minify_json,
|
| 18 |
[gr.Textbox(label="Input JSON Text"), gr.File(label="Upload JSON File")],
|
|
@@ -28,7 +31,6 @@ def minify_json_to_row(input_text, uploaded_file):
|
|
| 28 |
json_content = json.loads(input_text)
|
| 29 |
minified_rows = ["{"]
|
| 30 |
for key, value in json_content.items():
|
| 31 |
-
# Serialize value to JSON and append to list
|
| 32 |
serialized_value = json.dumps(value, separators=(',', ':'))
|
| 33 |
minified_rows.append(f' "{key}": {serialized_value}')
|
| 34 |
minified_rows.append("}")
|
|
@@ -36,13 +38,14 @@ def minify_json_to_row(input_text, uploaded_file):
|
|
| 36 |
except json.JSONDecodeError:
|
| 37 |
return "Invalid JSON input. Please provide a valid JSON."
|
| 38 |
|
|
|
|
| 39 |
minify_to_row = gr.Interface(
|
| 40 |
minify_json_to_row,
|
| 41 |
[gr.Textbox(label="Input JSON Text"), gr.File(label="Upload JSON File")],
|
| 42 |
"text"
|
| 43 |
)
|
| 44 |
|
| 45 |
-
# Function for the Should we translate? interface
|
| 46 |
def check_input(input_text, uploaded_file):
|
| 47 |
if uploaded_file is not None:
|
| 48 |
input_text = uploaded_file["data"].decode("utf-8")
|
|
@@ -53,14 +56,31 @@ def check_input(input_text, uploaded_file):
|
|
| 53 |
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]
|
| 54 |
return " ".join(response)
|
| 55 |
|
|
|
|
| 56 |
should_we_translate = gr.Interface(
|
| 57 |
check_input,
|
| 58 |
[gr.Textbox(label="Input Text"), gr.File(label="Upload File")],
|
| 59 |
"text"
|
| 60 |
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# Creating a Tabbed Interface with all functionalities
|
| 63 |
-
demo = gr.TabbedInterface([minify, should_we_translate, minify_to_row], ["Minify JSON", "Should we translate?", "Minify to row"])
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
+
from docx import Document
|
| 4 |
+
import io
|
| 5 |
|
| 6 |
# Function to minify JSON
|
| 7 |
def minify_json(input_text, uploaded_file):
|
|
|
|
| 15 |
except json.JSONDecodeError:
|
| 16 |
return "Invalid JSON input. Please provide a valid JSON."
|
| 17 |
|
| 18 |
+
# Gradio interface for minifying JSON
|
| 19 |
minify = gr.Interface(
|
| 20 |
minify_json,
|
| 21 |
[gr.Textbox(label="Input JSON Text"), gr.File(label="Upload JSON File")],
|
|
|
|
| 31 |
json_content = json.loads(input_text)
|
| 32 |
minified_rows = ["{"]
|
| 33 |
for key, value in json_content.items():
|
|
|
|
| 34 |
serialized_value = json.dumps(value, separators=(',', ':'))
|
| 35 |
minified_rows.append(f' "{key}": {serialized_value}')
|
| 36 |
minified_rows.append("}")
|
|
|
|
| 38 |
except json.JSONDecodeError:
|
| 39 |
return "Invalid JSON input. Please provide a valid JSON."
|
| 40 |
|
| 41 |
+
# Gradio interface for minifying JSON to rows
|
| 42 |
minify_to_row = gr.Interface(
|
| 43 |
minify_json_to_row,
|
| 44 |
[gr.Textbox(label="Input JSON Text"), gr.File(label="Upload JSON File")],
|
| 45 |
"text"
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Function for the 'Should we translate?' interface
|
| 49 |
def check_input(input_text, uploaded_file):
|
| 50 |
if uploaded_file is not None:
|
| 51 |
input_text = uploaded_file["data"].decode("utf-8")
|
|
|
|
| 56 |
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]
|
| 57 |
return " ".join(response)
|
| 58 |
|
| 59 |
+
# Gradio interface for checking if we should translate
|
| 60 |
should_we_translate = gr.Interface(
|
| 61 |
check_input,
|
| 62 |
[gr.Textbox(label="Input Text"), gr.File(label="Upload File")],
|
| 63 |
"text"
|
| 64 |
)
|
| 65 |
|
| 66 |
+
# Function to read text from a Word document
|
| 67 |
+
def read_word_document(uploaded_file):
|
| 68 |
+
if uploaded_file is not None:
|
| 69 |
+
file_stream = io.BytesIO(uploaded_file["data"])
|
| 70 |
+
document = Document(file_stream)
|
| 71 |
+
text = '\n'.join([para.text for para in document.paragraphs])
|
| 72 |
+
return text
|
| 73 |
+
return "Please upload a Word document."
|
| 74 |
+
|
| 75 |
+
# Gradio interface for reading Word document
|
| 76 |
+
read_docx = gr.Interface(
|
| 77 |
+
read_word_document,
|
| 78 |
+
gr.File(label="Upload Word Document"),
|
| 79 |
+
"text"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
# Creating a Tabbed Interface with all functionalities
|
| 83 |
+
demo = gr.TabbedInterface([minify, should_we_translate, minify_to_row, read_docx], ["Minify JSON", "Should we translate?", "Minify to row", "Template"])
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|
| 86 |
demo.launch()
|