Ahmed235 commited on
Commit
95d05cb
·
verified ·
1 Parent(s): 341ccc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -21
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
- extracted_text = extract_text_from_pptx(file_path)
29
- cleaned_text = re.sub(r'\s+', ' ', extracted_text)
 
30
 
31
- # Tokenize and encode the cleaned text
32
- input_encoding = tokenizer(cleaned_text, truncation=True, padding=True, return_tensors="pt")
33
- input_encoding = {key: val.to(device) for key, val in input_encoding.items()} # Move input tensor to CPU
34
 
35
- # Perform inference
36
- with torch.no_grad():
37
- outputs = model(**input_encoding)
38
- logits = outputs.logits
39
 
40
- probabilities = F.softmax(logits, dim=1)
41
 
42
- predicted_label_id = torch.argmax(logits, dim=1).item()
43
- predicted_label = model.config.id2label[predicted_label_id]
44
- predicted_probability = probabilities[0][predicted_label_id].item()
45
 
46
- # Summarize the cleaned text
47
- summary = summarizer(cleaned_text, max_length=80, min_length=30, do_sample=False)[0]['summary_text']
48
 
49
- prediction = {
50
- "Predicted Label": predicted_label,
51
- "Evaluation": f"Evaluate the topic according to {predicted_label} is: {predicted_probability}",
52
- "Summary": summary
53
- }
54
 
55
- return prediction
 
 
 
 
 
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(