Spaces:
Sleeping
Sleeping
update in files and ppt conversion to pdf
Browse files- app.py +4 -4
- ppt_preproc.py +130 -0
app.py
CHANGED
|
@@ -78,7 +78,7 @@ def setup_agent(vectorstore):
|
|
| 78 |
tools = [retriever_tool, web_tool, scenario_simulator]
|
| 79 |
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.3)
|
| 80 |
prompt = ChatPromptTemplate.from_messages([
|
| 81 |
-
("system", "You are Dr. Sue Cohen's Teaching Assistant, a passionate tutor for
|
| 82 |
("human", "{input}"),
|
| 83 |
MessagesPlaceholder("agent_scratchpad")
|
| 84 |
])
|
|
@@ -108,9 +108,9 @@ agent = setup_agent(vectorstore)
|
|
| 108 |
|
| 109 |
demo = gr.ChatInterface(
|
| 110 |
fn=chat,
|
| 111 |
-
title="
|
| 112 |
-
description="Query materials + web
|
| 113 |
)
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 78 |
tools = [retriever_tool, web_tool, scenario_simulator]
|
| 79 |
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.3)
|
| 80 |
prompt = ChatPromptTemplate.from_messages([
|
| 81 |
+
("system", "You are Dr. Sue Cohen's Teaching Assistant, a passionate tutor for Commercializing New Technologies, Leading Organizations to Innovate Smarter, Competing in Emerging Economies, and Strategic Management. Guide users with sharp insights, probing questions, and hints from course materials/web. Be witty (e.g., 'Solid, but don’t optimize for chaos—how’s this tie to diffusion?'), professional, encouraging. Run scenario sims for adoption/scalability queries with clear params. Prioritize course/web data, use web for updates. Be concise, cite sources, never solve outright, redirect if off-topic, prompt next steps."),
|
| 82 |
("human", "{input}"),
|
| 83 |
MessagesPlaceholder("agent_scratchpad")
|
| 84 |
])
|
|
|
|
| 108 |
|
| 109 |
demo = gr.ChatInterface(
|
| 110 |
fn=chat,
|
| 111 |
+
title="COMMERCIALIZING NEW TECHNOLOGIES RAG Bot",
|
| 112 |
+
description="Query materials + web augmented"
|
| 113 |
)
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
+
demo.launch(server_name="0.0.0.0", server_port=7860,share=True)
|
ppt_preproc.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import sys
|
| 4 |
+
import logging
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
# Set up logging
|
| 8 |
+
logging.basicConfig(
|
| 9 |
+
filename=f"conversion_log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log",
|
| 10 |
+
level=logging.INFO,
|
| 11 |
+
format="%(asctime)s - %(levelname)s - %(message)s"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# For .docx conversion
|
| 15 |
+
try:
|
| 16 |
+
from docx2pdf import convert as docx_convert
|
| 17 |
+
except ImportError:
|
| 18 |
+
logging.error("docx2pdf not installed. Run 'pip install docx2pdf'.")
|
| 19 |
+
sys.exit(1)
|
| 20 |
+
|
| 21 |
+
# For .pptx conversion
|
| 22 |
+
try:
|
| 23 |
+
import win32com.client
|
| 24 |
+
except ImportError:
|
| 25 |
+
logging.error("pywin32 not installed. Run 'pip install pywin32'.")
|
| 26 |
+
sys.exit(1)
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
import comtypes.client
|
| 30 |
+
except ImportError:
|
| 31 |
+
logging.warning("comtypes not installed. Run 'pip install comtypes' for fallback.")
|
| 32 |
+
|
| 33 |
+
def check_irm_status(filepath):
|
| 34 |
+
"""Check if IRM is enabled on a PPTX file."""
|
| 35 |
+
try:
|
| 36 |
+
ppt_app = win32com.client.Dispatch("PowerPoint.Application")
|
| 37 |
+
pres = ppt_app.Presentations.Open(os.path.abspath(filepath), ReadOnly=True, WithWindow=False)
|
| 38 |
+
irm_enabled = pres.Permission.Enabled
|
| 39 |
+
pres.Close()
|
| 40 |
+
ppt_app.Quit()
|
| 41 |
+
return irm_enabled
|
| 42 |
+
except Exception as e:
|
| 43 |
+
logging.error(f"Failed to check IRM for {filepath}: {e}")
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
def convert_pptx_print(filepath, output_pdf):
|
| 47 |
+
"""Attempt PPTX to PDF conversion using print-to-PDF."""
|
| 48 |
+
try:
|
| 49 |
+
ppt_app = win32com.client.Dispatch("PowerPoint.Application")
|
| 50 |
+
pres = ppt_app.Presentations.Open(os.path.abspath(filepath), ReadOnly=True, WithWindow=False)
|
| 51 |
+
pres.PrintOut(PrintToFile=output_pdf)
|
| 52 |
+
pres.Close()
|
| 53 |
+
ppt_app.Quit()
|
| 54 |
+
logging.info(f"Converted {filepath} to {output_pdf} using print-to-PDF.")
|
| 55 |
+
return True
|
| 56 |
+
except Exception as e:
|
| 57 |
+
logging.error(f"Print-to-PDF failed for {filepath}: {e}")
|
| 58 |
+
return False
|
| 59 |
+
|
| 60 |
+
def convert_pptx_win32(filepath, output_pdf):
|
| 61 |
+
"""Attempt PPTX to PDF conversion using win32com SaveAs."""
|
| 62 |
+
try:
|
| 63 |
+
ppt_app = win32com.client.Dispatch("PowerPoint.Application")
|
| 64 |
+
pres = ppt_app.Presentations.Open(os.path.abspath(filepath), ReadOnly=True, WithWindow=False)
|
| 65 |
+
pres.SaveAs(output_pdf, 32) # 32 = ppSaveAsPDF
|
| 66 |
+
pres.Close()
|
| 67 |
+
ppt_app.Quit()
|
| 68 |
+
logging.info(f"Converted {filepath} to {output_pdf} using win32com SaveAs.")
|
| 69 |
+
return True
|
| 70 |
+
except Exception as e:
|
| 71 |
+
logging.error(f"win32com SaveAs failed for {filepath}: {e}")
|
| 72 |
+
return False
|
| 73 |
+
|
| 74 |
+
def convert_pptx_comtypes(filepath, output_pdf):
|
| 75 |
+
"""Attempt PPTX to PDF conversion using comtypes SaveAs."""
|
| 76 |
+
try:
|
| 77 |
+
ppt_app = comtypes.client.CreateObject("PowerPoint.Application")
|
| 78 |
+
pres = ppt_app.Presentations.Open(os.path.abspath(filepath), ReadOnly=True, WithWindow=False)
|
| 79 |
+
pres.SaveAs(output_pdf, 32) # 32 = ppSaveAsPDF
|
| 80 |
+
pres.Close()
|
| 81 |
+
ppt_app.Quit()
|
| 82 |
+
logging.info(f"Converted {filepath} to {output_pdf} using comtypes SaveAs.")
|
| 83 |
+
return True
|
| 84 |
+
except Exception as e:
|
| 85 |
+
logging.error(f"comtypes SaveAs failed for {filepath}: {e}")
|
| 86 |
+
return False
|
| 87 |
+
|
| 88 |
+
def main(input_dir, output_dir):
|
| 89 |
+
if not os.path.isdir(input_dir):
|
| 90 |
+
logging.error(f"{input_dir} is not a directory.")
|
| 91 |
+
sys.exit(1)
|
| 92 |
+
|
| 93 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 94 |
+
|
| 95 |
+
for root, _, files in os.walk(input_dir):
|
| 96 |
+
rel_dir = os.path.relpath(root, input_dir)
|
| 97 |
+
target_subdir = os.path.join(output_dir, rel_dir)
|
| 98 |
+
os.makedirs(target_subdir, exist_ok=True)
|
| 99 |
+
|
| 100 |
+
for file in files:
|
| 101 |
+
filepath = os.path.join(root, file)
|
| 102 |
+
base, ext = os.path.splitext(file)
|
| 103 |
+
output_pdf = os.path.join(target_subdir, base + '.pdf')
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
if ext.lower() == '.docx':
|
| 107 |
+
docx_convert(filepath, output_pdf)
|
| 108 |
+
logging.info(f"Converted {filepath} to {output_pdf}")
|
| 109 |
+
elif ext.lower() == '.pptx':
|
| 110 |
+
# Check IRM status
|
| 111 |
+
irm_status = check_irm_status(filepath)
|
| 112 |
+
if irm_status is not None:
|
| 113 |
+
logging.info(f"IRM enabled for {filepath}: {irm_status}")
|
| 114 |
+
|
| 115 |
+
# Try print-to-PDF first (bypasses edit restrictions)
|
| 116 |
+
if not convert_pptx_print(filepath, output_pdf):
|
| 117 |
+
# Fallback to SaveAs methods
|
| 118 |
+
if not convert_pptx_win32(filepath, output_pdf):
|
| 119 |
+
if 'comtypes' in sys.modules and not convert_pptx_comtypes(filepath, output_pdf):
|
| 120 |
+
logging.warning(f"Failed to convert {filepath}. Likely IRM or strict restrictions.")
|
| 121 |
+
elif not 'comtypes' in sys.modules:
|
| 122 |
+
logging.warning(f"Failed to convert {filepath}. Install comtypes for additional fallback.")
|
| 123 |
+
elif ext.lower() == '.pdf':
|
| 124 |
+
shutil.copy(filepath, output_pdf)
|
| 125 |
+
logging.info(f"Copied {filepath} to {output_pdf}")
|
| 126 |
+
except Exception as e:
|
| 127 |
+
logging.error(f"Error processing {filepath}: {e}")
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
main("OrigFolder", "ConvertedFiles") # Use OrigFolder directly
|