Spaces:
Sleeping
Sleeping
import gradio as gr | |
from database import save_to_db | |
from preprocessing import read_file | |
def process_file(file, topics): | |
""" | |
Processes an uploaded file, extracts its text content, and saves it to the database. | |
This function performs the following steps: | |
1. Reads the content of the uploaded file using the `read_file` function. | |
- Supports `.docx`, `.txt`, and `.pdf` file formats. | |
2. Splits the extracted text into chunks (if applicable). | |
3. Saves the processed text and associated topics to the database using the `save_to_db` function. | |
4. Returns a success message if the file is processed and saved successfully. | |
If any error occurs during processing, the function catches the exception and returns an error message. | |
Parameters: | |
---------- | |
file : object | |
The uploaded file object. The file's name (`file.name`) is used to determine the file path. | |
topics : list or str | |
A list of topics or a single topic string associated with the file. These are saved to the database along with the file content. | |
Returns: | |
------- | |
str | |
- A success message indicating that the file was processed and saved successfully. | |
- An error message if an exception occurs during processing. | |
Example: | |
-------- | |
>>> process_file(uploaded_file, ["Persian Literature", "History"]) | |
'File processed successfully! File saved to the database.' | |
>>> process_file(unsupported_file, ["Science"]) | |
'Error processing file: Unsupported file format. Only .docx, .txt, and .pdf are allowed.' | |
""" | |
try: | |
# Read the file content | |
file_path = file.name | |
text = read_file(file_path) | |
# Spl | |
# Save chunks to database | |
save_to_db(text, topics) | |
return f"File processed successfully! file saved to the database." | |
except Exception as e: | |
return f"Error processing file: {str(e)}" | |
# Define Gradio interface | |
with gr.Blocks() as demo: | |
gr.Markdown("# Dataset Upload Interface") | |
with gr.Row(): | |
file_input = gr.File(label="Upload File (.docx or .txt or .pdf)") | |
topics_input = gr.Textbox(label="Topics (comma-separated)", placeholder="e.g., science, technology, law, medicin") | |
submit_button = gr.Button("Upload and Process") | |
output_text = gr.Textbox(label="Status") | |
submit_button.click(process_file, inputs=[file_input, topics_input], outputs=output_text) | |
# Launch the app | |
demo.launch() |