Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,106 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
-
|
| 5 |
from PIL import Image
|
| 6 |
import torch
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# β
Hugging Face
|
| 10 |
hf_token = os.getenv("HF_TOKEN")
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
device_map="auto",
|
| 18 |
-
|
| 19 |
)
|
| 20 |
|
| 21 |
-
# β
|
| 22 |
extracted_text = ""
|
| 23 |
-
|
| 24 |
|
| 25 |
def extract_text_from_pptx_json(parsed_json: dict) -> str:
|
| 26 |
text = ""
|
| 27 |
for slide in parsed_json.values():
|
| 28 |
for shape in slide.values():
|
| 29 |
-
if shape.get(
|
| 30 |
-
for group_shape in shape.get(
|
| 31 |
-
if group_shape.get(
|
| 32 |
for para_key, para in group_shape.items():
|
| 33 |
if para_key.startswith("paragraph_"):
|
| 34 |
text += para.get("text", "") + "\n"
|
| 35 |
-
elif shape.get(
|
| 36 |
for para_key, para in shape.items():
|
| 37 |
if para_key.startswith("paragraph_"):
|
| 38 |
text += para.get("text", "") + "\n"
|
| 39 |
return text.strip()
|
| 40 |
|
| 41 |
-
# β
Handle uploaded
|
| 42 |
def handle_pptx_upload(pptx_file):
|
| 43 |
-
global extracted_text,
|
| 44 |
tmp_path = pptx_file.name
|
| 45 |
parsed_json_str, image_paths = transfer_to_structure(tmp_path, "images")
|
| 46 |
parsed_json = json.loads(parsed_json_str)
|
| 47 |
extracted_text = extract_text_from_pptx_json(parsed_json)
|
| 48 |
-
slide_images = image_paths
|
| 49 |
return extracted_text or "No readable text found in slides."
|
| 50 |
|
| 51 |
-
# β
|
| 52 |
def ask_llama(question):
|
| 53 |
-
global extracted_text,
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# β
Gradio UI
|
| 78 |
with gr.Blocks() as demo:
|
| 79 |
-
gr.Markdown("## π§ Llama 4 Scout
|
| 80 |
|
| 81 |
pptx_input = gr.File(label="π Upload PPTX File", file_types=[".pptx"])
|
| 82 |
-
extract_btn = gr.Button("π Extract Text +
|
| 83 |
|
| 84 |
-
extracted_output = gr.Textbox(label="π
|
| 85 |
|
| 86 |
extract_btn.click(handle_pptx_upload, inputs=[pptx_input], outputs=[extracted_output])
|
| 87 |
|
| 88 |
question = gr.Textbox(label="β Ask a Question")
|
| 89 |
ask_btn = gr.Button("π¬ Ask Llama 4 Scout")
|
| 90 |
-
ai_answer = gr.Textbox(label="π€
|
| 91 |
|
| 92 |
ask_btn.click(ask_llama, inputs=[question], outputs=[ai_answer])
|
| 93 |
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
import requests
|
| 4 |
from PIL import Image
|
| 5 |
import torch
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from ppt_parser import transfer_to_structure
|
| 8 |
+
from transformers import AutoProcessor, Llama4ForConditionalGeneration
|
| 9 |
|
| 10 |
+
# β
Hugging Face token
|
| 11 |
hf_token = os.getenv("HF_TOKEN")
|
| 12 |
+
model_id = "meta-llama/Llama-4-Scout-17B-16E-Instruct"
|
| 13 |
+
|
| 14 |
+
# β
Load model & processor
|
| 15 |
+
processor = AutoProcessor.from_pretrained(model_id, token=hf_token)
|
| 16 |
+
model = Llama4ForConditionalGeneration.from_pretrained(
|
| 17 |
+
model_id,
|
| 18 |
+
token=hf_token,
|
| 19 |
+
attn_implementation="flex_attention",
|
| 20 |
device_map="auto",
|
| 21 |
+
torch_dtype=torch.bfloat16,
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# β
Global storage
|
| 25 |
extracted_text = ""
|
| 26 |
+
image_paths = []
|
| 27 |
|
| 28 |
def extract_text_from_pptx_json(parsed_json: dict) -> str:
|
| 29 |
text = ""
|
| 30 |
for slide in parsed_json.values():
|
| 31 |
for shape in slide.values():
|
| 32 |
+
if shape.get("type") == "group":
|
| 33 |
+
for group_shape in shape.get("group_content", {}).values():
|
| 34 |
+
if group_shape.get("type") == "text":
|
| 35 |
for para_key, para in group_shape.items():
|
| 36 |
if para_key.startswith("paragraph_"):
|
| 37 |
text += para.get("text", "") + "\n"
|
| 38 |
+
elif shape.get("type") == "text":
|
| 39 |
for para_key, para in shape.items():
|
| 40 |
if para_key.startswith("paragraph_"):
|
| 41 |
text += para.get("text", "") + "\n"
|
| 42 |
return text.strip()
|
| 43 |
|
| 44 |
+
# β
Handle uploaded PPTX
|
| 45 |
def handle_pptx_upload(pptx_file):
|
| 46 |
+
global extracted_text, image_paths
|
| 47 |
tmp_path = pptx_file.name
|
| 48 |
parsed_json_str, image_paths = transfer_to_structure(tmp_path, "images")
|
| 49 |
parsed_json = json.loads(parsed_json_str)
|
| 50 |
extracted_text = extract_text_from_pptx_json(parsed_json)
|
|
|
|
| 51 |
return extracted_text or "No readable text found in slides."
|
| 52 |
|
| 53 |
+
# β
Multimodal Q&A using Scout
|
| 54 |
def ask_llama(question):
|
| 55 |
+
global extracted_text, image_paths
|
| 56 |
+
|
| 57 |
+
if not extracted_text and not image_paths:
|
| 58 |
+
return "Please upload and extract a PPTX first."
|
| 59 |
+
|
| 60 |
+
# π§ Build multimodal chat messages
|
| 61 |
+
messages = [
|
| 62 |
+
{
|
| 63 |
+
"role": "user",
|
| 64 |
+
"content": [],
|
| 65 |
+
}
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
# Add up to 2 images to prevent OOM
|
| 69 |
+
for path in image_paths[:2]:
|
| 70 |
+
messages[0]["content"].append({"type": "image", "image": Image.open(path)})
|
| 71 |
+
|
| 72 |
+
messages[0]["content"].append({
|
| 73 |
+
"type": "text",
|
| 74 |
+
"text": f"{extracted_text}\n\nQuestion: {question}"
|
| 75 |
+
})
|
| 76 |
+
|
| 77 |
+
inputs = processor.apply_chat_template(
|
| 78 |
+
messages,
|
| 79 |
+
add_generation_prompt=True,
|
| 80 |
+
tokenize=True,
|
| 81 |
+
return_dict=True,
|
| 82 |
+
return_tensors="pt"
|
| 83 |
+
).to(model.device)
|
| 84 |
+
|
| 85 |
+
outputs = model.generate(**inputs, max_new_tokens=256)
|
| 86 |
+
|
| 87 |
+
response = processor.batch_decode(outputs[:, inputs["input_ids"].shape[-1]:])[0]
|
| 88 |
+
return response.strip()
|
| 89 |
|
| 90 |
# β
Gradio UI
|
| 91 |
with gr.Blocks() as demo:
|
| 92 |
+
gr.Markdown("## π§ Multimodal Llama 4 Scout Study Assistant")
|
| 93 |
|
| 94 |
pptx_input = gr.File(label="π Upload PPTX File", file_types=[".pptx"])
|
| 95 |
+
extract_btn = gr.Button("π Extract Text + Images")
|
| 96 |
|
| 97 |
+
extracted_output = gr.Textbox(label="π Slide Text", lines=10, interactive=False)
|
| 98 |
|
| 99 |
extract_btn.click(handle_pptx_upload, inputs=[pptx_input], outputs=[extracted_output])
|
| 100 |
|
| 101 |
question = gr.Textbox(label="β Ask a Question")
|
| 102 |
ask_btn = gr.Button("π¬ Ask Llama 4 Scout")
|
| 103 |
+
ai_answer = gr.Textbox(label="π€ Answer", lines=6)
|
| 104 |
|
| 105 |
ask_btn.click(ask_llama, inputs=[question], outputs=[ai_answer])
|
| 106 |
|