muhammadrazapathan commited on
Commit
f048c9c
·
verified ·
1 Parent(s): 6852493

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import fitz # PyMuPDF
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+ import re
6
+
7
+ def analyze_pdf(file):
8
+ if file is None:
9
+ return "Please upload a PDF.", None
10
+
11
+ doc = fitz.open(stream=file.read(), filetype="pdf")
12
+
13
+ text = ""
14
+ for page in doc:
15
+ text += page.get_text()
16
+
17
+ words = re.findall(r"\b[A-Za-z]{4,}\b", text.lower())
18
+
19
+ if len(words) == 0:
20
+ return "No readable text found.", None
21
+
22
+ freq = pd.Series(words).value_counts().head(10)
23
+
24
+ plt.figure(figsize=(6,4))
25
+ freq.plot(kind="bar")
26
+ plt.title("Top Keywords in PDF")
27
+ plt.tight_layout()
28
+
29
+ path = "plot.png"
30
+ plt.savefig(path)
31
+ plt.close()
32
+
33
+ return freq.to_string(), path
34
+
35
+
36
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
37
+ gr.Markdown("## 📊 College Exam Trend Analyzer")
38
+
39
+ file_input = gr.File(label="Upload PDF")
40
+ text_output = gr.Textbox(label="Keyword Analysis")
41
+ img_output = gr.Image(label="Visualization")
42
+
43
+ btn = gr.Button("Analyze")
44
+
45
+ btn.click(analyze_pdf, inputs=file_input, outputs=[text_output, img_output])
46
+
47
+ demo.launch()