Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,20 @@ import gradio as gr
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
from transformers import pipeline
|
| 4 |
import json
|
| 5 |
-
from
|
| 6 |
-
from ppt_parser import transfer_to_structure # <- FIXED import
|
| 7 |
|
| 8 |
-
# β
|
| 9 |
GOOGLE_API_KEY = "AIzaSyA8fWpwJE21zxpuN8Fi8Qx9-iwx3d_AZiw"
|
| 10 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 11 |
|
|
|
|
| 12 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 13 |
gemini_model = genai.GenerativeModel("models/gemini-1.5-flash")
|
| 14 |
|
|
|
|
| 15 |
extracted_text = ""
|
| 16 |
|
|
|
|
| 17 |
def extract_text_from_pptx_json(parsed_json: dict) -> str:
|
| 18 |
extracted_text = ""
|
| 19 |
for slide_key, slide in parsed_json.items():
|
|
@@ -31,17 +33,17 @@ def extract_text_from_pptx_json(parsed_json: dict) -> str:
|
|
| 31 |
extracted_text += para.get("text", "") + "\n"
|
| 32 |
return extracted_text.strip()
|
| 33 |
|
|
|
|
| 34 |
def handle_pptx_upload(pptx_file):
|
| 35 |
global extracted_text
|
| 36 |
-
|
| 37 |
-
tmp.write(pptx_file.read())
|
| 38 |
-
tmp_path = tmp.name
|
| 39 |
|
| 40 |
parsed_json_str, _, _ = transfer_to_structure(tmp_path, "images")
|
| 41 |
parsed_json = json.loads(parsed_json_str)
|
| 42 |
extracted_text = extract_text_from_pptx_json(parsed_json)
|
| 43 |
return extracted_text or "No readable text found in slides."
|
| 44 |
|
|
|
|
| 45 |
def summarize_text():
|
| 46 |
global extracted_text
|
| 47 |
if not extracted_text:
|
|
@@ -49,6 +51,7 @@ def summarize_text():
|
|
| 49 |
summary = summarizer(extracted_text, max_length=200, min_length=50, do_sample=False)[0]['summary_text']
|
| 50 |
return summary
|
| 51 |
|
|
|
|
| 52 |
def clarify_concept(question):
|
| 53 |
global extracted_text
|
| 54 |
if not extracted_text:
|
|
@@ -57,11 +60,11 @@ def clarify_concept(question):
|
|
| 57 |
response = gemini_model.generate_content(prompt)
|
| 58 |
return response.text if response else "No response from Gemini."
|
| 59 |
|
| 60 |
-
# β
Gradio
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
gr.Markdown("## π§ AI-Powered Study Assistant for PowerPoint Lectures")
|
| 63 |
|
| 64 |
-
pptx_input = gr.File(label="π Upload
|
| 65 |
extract_btn = gr.Button("π Extract & Summarize")
|
| 66 |
|
| 67 |
extracted_output = gr.Textbox(label="π Extracted Text", lines=10, interactive=False)
|
|
@@ -76,5 +79,6 @@ with gr.Blocks() as demo:
|
|
| 76 |
|
| 77 |
ask_btn.click(clarify_concept, inputs=[question], outputs=[ai_answer])
|
| 78 |
|
|
|
|
| 79 |
if __name__ == "__main__":
|
| 80 |
-
demo.launch(
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
from transformers import pipeline
|
| 4 |
import json
|
| 5 |
+
from ppt_parser import transfer_to_structure # updated and working
|
|
|
|
| 6 |
|
| 7 |
+
# β
Your Google Gemini API Key
|
| 8 |
GOOGLE_API_KEY = "AIzaSyA8fWpwJE21zxpuN8Fi8Qx9-iwx3d_AZiw"
|
| 9 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 10 |
|
| 11 |
+
# β
Load Models
|
| 12 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 13 |
gemini_model = genai.GenerativeModel("models/gemini-1.5-flash")
|
| 14 |
|
| 15 |
+
# β
Global variable to hold extracted text
|
| 16 |
extracted_text = ""
|
| 17 |
|
| 18 |
+
# β
Flatten extracted JSON into plain text
|
| 19 |
def extract_text_from_pptx_json(parsed_json: dict) -> str:
|
| 20 |
extracted_text = ""
|
| 21 |
for slide_key, slide in parsed_json.items():
|
|
|
|
| 33 |
extracted_text += para.get("text", "") + "\n"
|
| 34 |
return extracted_text.strip()
|
| 35 |
|
| 36 |
+
# β
Main file handler
|
| 37 |
def handle_pptx_upload(pptx_file):
|
| 38 |
global extracted_text
|
| 39 |
+
tmp_path = pptx_file.name # Fix for NamedString error on Spaces
|
|
|
|
|
|
|
| 40 |
|
| 41 |
parsed_json_str, _, _ = transfer_to_structure(tmp_path, "images")
|
| 42 |
parsed_json = json.loads(parsed_json_str)
|
| 43 |
extracted_text = extract_text_from_pptx_json(parsed_json)
|
| 44 |
return extracted_text or "No readable text found in slides."
|
| 45 |
|
| 46 |
+
# β
Summary generator
|
| 47 |
def summarize_text():
|
| 48 |
global extracted_text
|
| 49 |
if not extracted_text:
|
|
|
|
| 51 |
summary = summarizer(extracted_text, max_length=200, min_length=50, do_sample=False)[0]['summary_text']
|
| 52 |
return summary
|
| 53 |
|
| 54 |
+
# β
Gemini-powered Q&A
|
| 55 |
def clarify_concept(question):
|
| 56 |
global extracted_text
|
| 57 |
if not extracted_text:
|
|
|
|
| 60 |
response = gemini_model.generate_content(prompt)
|
| 61 |
return response.text if response else "No response from Gemini."
|
| 62 |
|
| 63 |
+
# β
Gradio UI
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
gr.Markdown("## π§ AI-Powered Study Assistant for PowerPoint Lectures")
|
| 66 |
|
| 67 |
+
pptx_input = gr.File(label="π Upload PPTX File", file_types=[".pptx"]) # Fix mobile upload
|
| 68 |
extract_btn = gr.Button("π Extract & Summarize")
|
| 69 |
|
| 70 |
extracted_output = gr.Textbox(label="π Extracted Text", lines=10, interactive=False)
|
|
|
|
| 79 |
|
| 80 |
ask_btn.click(clarify_concept, inputs=[question], outputs=[ai_answer])
|
| 81 |
|
| 82 |
+
# β
Launch app (without share=True for Spaces)
|
| 83 |
if __name__ == "__main__":
|
| 84 |
+
demo.launch()
|