Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,87 +0,0 @@
|
|
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 app import transfer_to_structure # from your pptx-parser
|
7 |
-
|
8 |
-
# β
Use your Gemini API Key
|
9 |
-
GOOGLE_API_KEY = "AIzaSyA8fWpwJE21zxpuN8Fi8Qx9-iwx3d_AZiw"
|
10 |
-
genai.configure(api_key=GOOGLE_API_KEY)
|
11 |
-
|
12 |
-
# β
Load Summarization Model
|
13 |
-
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
14 |
-
gemini_model = genai.GenerativeModel("models/gemini-1.5-flash")
|
15 |
-
|
16 |
-
# β
Global text storage
|
17 |
-
extracted_text = ""
|
18 |
-
|
19 |
-
# β
Function to flatten PPTX-parsed JSON into readable text
|
20 |
-
def extract_text_from_pptx_json(parsed_json: dict) -> str:
|
21 |
-
extracted_text = ""
|
22 |
-
for slide_key, slide in parsed_json.items():
|
23 |
-
for shape_key, shape in slide.items():
|
24 |
-
if shape.get('type') == 'group':
|
25 |
-
group = shape.get('group_content', {})
|
26 |
-
for _, group_shape in group.items():
|
27 |
-
if group_shape.get('type') == 'text':
|
28 |
-
for para_key, para in group_shape.items():
|
29 |
-
if para_key.startswith("paragraph_"):
|
30 |
-
extracted_text += para.get("text", "") + "\n"
|
31 |
-
elif shape.get('type') == 'text':
|
32 |
-
for para_key, para in shape.items():
|
33 |
-
if para_key.startswith("paragraph_"):
|
34 |
-
extracted_text += para.get("text", "") + "\n"
|
35 |
-
return extracted_text.strip()
|
36 |
-
|
37 |
-
# β
Handler for file upload & parsing
|
38 |
-
def handle_pptx_upload(pptx_file):
|
39 |
-
global extracted_text
|
40 |
-
with NamedTemporaryFile(delete=False, suffix=".pptx") as tmp:
|
41 |
-
tmp.write(pptx_file.read())
|
42 |
-
tmp_path = tmp.name
|
43 |
-
|
44 |
-
parsed_json_str, _, _ = transfer_to_structure(tmp_path, "images")
|
45 |
-
parsed_json = json.loads(parsed_json_str)
|
46 |
-
extracted_text = extract_text_from_pptx_json(parsed_json)
|
47 |
-
return extracted_text or "No readable text found in slides."
|
48 |
-
|
49 |
-
# β
Summarization function
|
50 |
-
def summarize_text():
|
51 |
-
global extracted_text
|
52 |
-
if not extracted_text:
|
53 |
-
return "Please upload and extract text from a PPTX file first."
|
54 |
-
summary = summarizer(extracted_text, max_length=200, min_length=50, do_sample=False)[0]['summary_text']
|
55 |
-
return summary
|
56 |
-
|
57 |
-
# β
Gemini-powered Q&A
|
58 |
-
def clarify_concept(question):
|
59 |
-
global extracted_text
|
60 |
-
if not extracted_text:
|
61 |
-
return "Please upload and extract text from a PPTX file first."
|
62 |
-
prompt = f"Context:\n{extracted_text}\n\nQuestion: {question}"
|
63 |
-
response = gemini_model.generate_content(prompt)
|
64 |
-
return response.text if response else "No response from Gemini."
|
65 |
-
|
66 |
-
# β
Gradio UI
|
67 |
-
with gr.Blocks() as demo:
|
68 |
-
gr.Markdown("## π§ AI-Powered Study Assistant for PowerPoint Lectures")
|
69 |
-
|
70 |
-
pptx_input = gr.File(label="π Upload PPTX File")
|
71 |
-
extract_btn = gr.Button("π Extract & Summarize")
|
72 |
-
|
73 |
-
extracted_output = gr.Textbox(label="π Extracted Text", lines=10, interactive=False)
|
74 |
-
summary_output = gr.Textbox(label="π Summary", interactive=False)
|
75 |
-
|
76 |
-
extract_btn.click(handle_pptx_upload, inputs=[pptx_input], outputs=[extracted_output])
|
77 |
-
extract_btn.click(summarize_text, outputs=[summary_output])
|
78 |
-
|
79 |
-
question = gr.Textbox(label="β Ask a Question")
|
80 |
-
ask_btn = gr.Button("π¬ Ask Gemini")
|
81 |
-
ai_answer = gr.Textbox(label="π€ Gemini Answer", lines=4)
|
82 |
-
|
83 |
-
ask_btn.click(clarify_concept, inputs=[question], outputs=[ai_answer])
|
84 |
-
|
85 |
-
# β
Run Gradio app
|
86 |
-
if __name__ == "__main__":
|
87 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|