from pptx import Presentation import gradio as gr from pdf2image import convert_from_path import pdfplumber from docx import Document import subprocess import os from typing import Optional, List import string import random import re def extract_text_from_pptx(file_path): prs = Presentation(file_path) text_content = [] for slide in prs.slides: slide_text = [] for shape in slide.shapes: if hasattr(shape, "text"): slide_text.append(shape.text) text_content.append("\n".join(slide_text)) return "\n\n".join(text_content) def extract_text_from_ppt(file_path): try: # Convert PPT to PPTX using unoconv pptx_file_path = os.path.splitext(file_path)[0] + ".pptx" subprocess.run(["unoconv", "-f", "pptx", file_path], check=True) # Extract text from PPTX presentation = Presentation(pptx_file_path) text_content = [] for slide in presentation.slides: slide_text = [] for shape in slide.shapes: if hasattr(shape, "text"): slide_text.append(shape.text) text_content.append("\n".join(slide_text)) # Remove the converted PPTX file os.remove(pptx_file_path) return "\n\n".join(text_content) except Exception as e: print(f"Error extracting text from PPT file: {e}") return "Error extracting text from PPT file" def extract_text_from_ppt_or_pptx(file_path): if file_path.endswith(".pptx"): return extract_text_from_pptx(file_path) elif file_path.endswith(".ppt"): return extract_text_from_ppt(file_path) else: return "Unsupported file type. Please provide a .ppt or .pptx file." def convert_pdf_to_image(file): images = convert_from_path(file) return images def extract_text_from_pdf(file): text = "" with pdfplumber.open(file) as pdf: for page in pdf.pages: text += page.extract_text() + "\n" return text def extract_text_from_docx(file): text = "" doc = Document(file.name) for paragraph in doc.paragraphs: text += paragraph.text + "\n" return text def convert_doc_to_text(doc_path): try: subprocess.run( ["unoconv", "--format", "txt", doc_path], capture_output=True, text=True, check=True, ) txt_file_path = doc_path.replace(".doc", ".txt") with open(txt_file_path, "r") as f: text = f.read() text = text.lstrip("\ufeff") os.remove(txt_file_path) return text except subprocess.CalledProcessError as e: print(f"Error converting {doc_path} to text: {e}") return "" def extract_text_from_doc_or_docx(file): if file.name.endswith(".docx"): return extract_text_from_docx(file) elif file.name.endswith(".doc"): return convert_doc_to_text(file.name) else: return "Unsupported file type. Please upload a .doc or .docx file." # function that generates a random string def generate_random_string(length=23): characters = string.ascii_letters + string.digits # Includes letters and digits random_string = "".join(random.choice(characters) for _ in range(length)) return random_string # function that adds the necessary json fields def handle_json_output(json_list: list): n = len(json_list) for i in range(n): # not last element random_string1 = generate_random_string() random_string2 = generate_random_string() element = json_list[i] front = element["frontText"] back = element["backText"] element["frontHTML"] = ( f'
{front}
{back}
{back}
' front_html = re.sub(r"_{2,}", replace_cloze, front) element["frontHTML"] = ( f'
{front_html}