advaith2909 commited on
Commit
6abc153
·
verified ·
1 Parent(s): f4bcb3f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ python -m spacy download en_core_web_sm
2
+ from huggingface_hub import login
3
+ import os
4
+ import PyPDF2
5
+ import spacy
6
+ import nltk
7
+ from transformers import pipeline
8
+ import whisper
9
+ import json
10
+ from sklearn.feature_extraction.text import TfidfVectorizer
11
+ import numpy as np
12
+ import pandas as pd
13
+ import re
14
+ from textblob import TextBlob
15
+ from spacy import displacy
16
+ import gradio as gr
17
+ # Initialize spaCy model and other NLP tools
18
+ nlp = spacy.load("en_core_web_sm")
19
+
20
+ # Download the Gemma 2 model
21
+ summarizer = pipeline("summarization", model="google/gemma-2-2b-it")
22
+
23
+ # Text preprocessing
24
+ def preprocess_text(text):
25
+ doc = nlp(text)
26
+ tokens = [token.text for token in doc if not token.is_stop and not token.is_punct]
27
+ cleaned_text = " ".join(tokens)
28
+ return cleaned_text
29
+
30
+ # Text summarization
31
+ def summarize_text(text):
32
+ summary = summarizer(text, max_length=400, min_length=50, do_sample=False)
33
+ return summary[0]['summary_text']
34
+
35
+ # Sentiment analysis
36
+ def sentiment_analysis(text):
37
+ blob = TextBlob(text)
38
+ sentiment = blob.sentiment.polarity
39
+ if sentiment > 0:
40
+ return "Positive"
41
+ elif sentiment < 0:
42
+ return "Negative"
43
+ else:
44
+ return "Neutral"
45
+
46
+ # Keyword extraction
47
+ def extract_keywords(text):
48
+ vectorizer = TfidfVectorizer(stop_words='english')
49
+ tfidf_matrix = vectorizer.fit_transform([text])
50
+ feature_names = np.array(vectorizer.get_feature_names_out())
51
+ sorted_idx = tfidf_matrix.sum(axis=0).argsort()[::-1]
52
+ top_keywords = feature_names[sorted_idx[:10]]
53
+ return top_keywords.tolist()
54
+
55
+ # Decision/action item extraction
56
+ def extract_decisions(text):
57
+ doc = nlp(text)
58
+ decisions = []
59
+ for sent in doc.sents:
60
+ for token in sent:
61
+ if token.dep_ == "ROOT" and token.pos_ == "VERB":
62
+ decisions.append(sent.text)
63
+ return decisions
64
+
65
+
66
+ # Backend function to handle uploaded file
67
+ def handle_file_upload(uploaded_file):
68
+ if uploaded_file:
69
+ # Extract text from the PDF
70
+ pdf_reader = PyPDF2.PdfReader(uploaded_file)
71
+ text = ""
72
+ for page in pdf_reader.pages:
73
+ text += page.extract_text()
74
+
75
+ # Preprocess text
76
+ cleaned_text = preprocess_text(text)
77
+
78
+ # Summarize text
79
+ summary = summarize_text(cleaned_text)
80
+
81
+ # Sentiment analysis
82
+ sentiment = sentiment_analysis(text)
83
+
84
+ # Extract Keywords
85
+ keywords = extract_keywords(text)
86
+
87
+ # Extract decisions/action items
88
+ decisions = extract_decisions(text)
89
+
90
+ return {
91
+ 'summary': summary,
92
+ 'sentiment': sentiment,
93
+ 'keywords': keywords,
94
+ 'decisions': decisions
95
+ }
96
+ else:
97
+ return None
98
+
99
+
100
+ # Gradio Interface
101
+ def process_file(file):
102
+ if file is not None:
103
+ results = handle_file_upload(file)
104
+ if results:
105
+ return (
106
+ results['summary'],
107
+ results['sentiment'],
108
+ ", ".join(map(str, results['keywords'])),
109
+ "\n".join(results['decisions'])
110
+ )
111
+ return "No file uploaded!", "N/A", "N/A", "N/A"
112
+
113
+ # Define Gradio interface
114
+ interface = gr.Interface(
115
+ fn=process_file,
116
+ inputs=gr.File(label="Upload a PDF File"),
117
+ outputs=[
118
+ gr.Textbox(label="Summary"),
119
+ gr.Textbox(label="Sentiment Analysis"),
120
+ gr.Textbox(label="Keywords"),
121
+ gr.Textbox(label="Decisions/Action Items")
122
+ ],
123
+ title="Smart Meeting Summarizer",
124
+ description="Upload your meeting notes or PDF file to get a summary, sentiment analysis, keywords, and decisions/action items."
125
+ )
126
+
127
+ # Launch the Gradio app
128
+ interface.launch(debug=True)