Update app.py
Browse files
app.py
CHANGED
@@ -25,34 +25,40 @@ def extract_text_from_pptx(file_path):
|
|
25 |
return "\n".join(text)
|
26 |
|
27 |
def predict_pptx_content(file_path):
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
|
46 |
-
|
47 |
-
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
# Define the Gradio interface
|
58 |
iface = gr.Interface(
|
|
|
25 |
return "\n".join(text)
|
26 |
|
27 |
def predict_pptx_content(file_path):
|
28 |
+
try:
|
29 |
+
extracted_text = extract_text_from_pptx(file_path)
|
30 |
+
cleaned_text = re.sub(r'\s+', ' ', extracted_text)
|
31 |
|
32 |
+
# Tokenize and encode the cleaned text
|
33 |
+
input_encoding = tokenizer(cleaned_text, truncation=True, padding=True, return_tensors="pt")
|
34 |
+
input_encoding = {key: val.to(device) for key, val in input_encoding.items()} # Move input tensor to CPU
|
35 |
|
36 |
+
# Perform inference
|
37 |
+
with torch.no_grad():
|
38 |
+
outputs = model(**input_encoding)
|
39 |
+
logits = outputs.logits
|
40 |
|
41 |
+
probabilities = F.softmax(logits, dim=1)
|
42 |
|
43 |
+
predicted_label_id = torch.argmax(logits, dim=1).item()
|
44 |
+
predicted_label = model.config.id2label[predicted_label_id]
|
45 |
+
predicted_probability = probabilities[0][predicted_label_id].item()
|
46 |
|
47 |
+
# Summarize the cleaned text
|
48 |
+
summary = summarizer(cleaned_text, max_length=80, min_length=30, do_sample=False)[0]['summary_text']
|
49 |
|
50 |
+
prediction = {
|
51 |
+
"Predicted Label": predicted_label,
|
52 |
+
"Evaluation": f"Evaluate the topic according to {predicted_label} is: {predicted_probability}",
|
53 |
+
"Summary": summary
|
54 |
+
}
|
55 |
|
56 |
+
return prediction
|
57 |
+
|
58 |
+
except Exception as e:
|
59 |
+
# Log the error details
|
60 |
+
print(f"Error in predict_pptx_content: {e}")
|
61 |
+
return {"error": str(e)}
|
62 |
|
63 |
# Define the Gradio interface
|
64 |
iface = gr.Interface(
|