Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import google.generativeai as genai
|
3 |
+
from transformers import pipeline
|
4 |
+
import json
|
5 |
+
from tempfile import NamedTemporaryFile
|
6 |
+
from ppt_parser import transfer_to_structure # <- FIXED import
|
7 |
+
|
8 |
+
# β
Use your Gemini API key
|
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():
|
20 |
+
for shape_key, shape in slide.items():
|
21 |
+
if shape.get('type') == 'group':
|
22 |
+
group = shape.get('group_content', {})
|
23 |
+
for _, group_shape in group.items():
|
24 |
+
if group_shape.get('type') == 'text':
|
25 |
+
for para_key, para in group_shape.items():
|
26 |
+
if para_key.startswith("paragraph_"):
|
27 |
+
extracted_text += para.get("text", "") + "\n"
|
28 |
+
elif shape.get('type') == 'text':
|
29 |
+
for para_key, para in shape.items():
|
30 |
+
if para_key.startswith("paragraph_"):
|
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 |
+
with NamedTemporaryFile(delete=False, suffix=".pptx") as tmp:
|
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:
|
48 |
+
return "Please upload and extract text from a PPTX file first."
|
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:
|
55 |
+
return "Please upload and extract text from a PPTX file first."
|
56 |
+
prompt = f"Context:\n{extracted_text}\n\nQuestion: {question}"
|
57 |
+
response = gemini_model.generate_content(prompt)
|
58 |
+
return response.text if response else "No response from Gemini."
|
59 |
+
|
60 |
+
# β
Gradio Interface
|
61 |
+
with gr.Blocks() as demo:
|
62 |
+
gr.Markdown("## π§ AI-Powered Study Assistant for PowerPoint Lectures")
|
63 |
+
|
64 |
+
pptx_input = gr.File(label="π Upload PPTX File")
|
65 |
+
extract_btn = gr.Button("π Extract & Summarize")
|
66 |
+
|
67 |
+
extracted_output = gr.Textbox(label="π Extracted Text", lines=10, interactive=False)
|
68 |
+
summary_output = gr.Textbox(label="π Summary", interactive=False)
|
69 |
+
|
70 |
+
extract_btn.click(handle_pptx_upload, inputs=[pptx_input], outputs=[extracted_output])
|
71 |
+
extract_btn.click(summarize_text, outputs=[summary_output])
|
72 |
+
|
73 |
+
question = gr.Textbox(label="β Ask a Question")
|
74 |
+
ask_btn = gr.Button("π¬ Ask Gemini")
|
75 |
+
ai_answer = gr.Textbox(label="π€ Gemini Answer", lines=4)
|
76 |
+
|
77 |
+
ask_btn.click(clarify_concept, inputs=[question], outputs=[ai_answer])
|
78 |
+
|
79 |
+
if __name__ == "__main__":
|
80 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|