AamirAli123 commited on
Commit
257e8f6
·
verified ·
1 Parent(s): 95f644d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +231 -0
app.py ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ import gradio as gr
3
+ import os
4
+ import base64
5
+ import numpy as np
6
+ from PIL import Image
7
+ import io
8
+ import pandas as pd
9
+ from dotenv import load_env
10
+
11
+ load_env()
12
+ api_key = os.getenv("OPENAI_API_KEY")
13
+ client = OpenAI(
14
+ # This is the default and can be omitted
15
+ api_key = api_key,
16
+ )
17
+
18
+ def analyze_feedback(feedback_text):
19
+ """Analyzes feedback text and returns a summary.
20
+
21
+ Args:
22
+ feedback_text (str): The text of the feedback to analyze.
23
+
24
+ Returns:
25
+ str: A summary of the feedback.
26
+ """
27
+ chat_completion = client.chat.completions.create(
28
+ messages=[
29
+ {"role": "system", "content": "You are a helpful assistant."},
30
+ {"role": "user", "content": feedback_text}
31
+ ]
32
+ ,
33
+ model="gpt-3.5-turbo"
34
+ )
35
+ return chat_completion.choices[0].message.content
36
+
37
+ def process_files(uploaded_files):
38
+ results = [["File Name", "Summary", "Areas of Improvement", "Actionable Objectives","Sentiment", "Themes"]]
39
+
40
+ for file_path in uploaded_files:
41
+ # Extract file name from the file path
42
+ file_name = os.path.basename(file_path)
43
+
44
+ # Open and read the file content
45
+ with open(file_path, 'r', encoding='utf-8') as file:
46
+ feedback_text = file.read()
47
+ #print(feedback_text)
48
+
49
+ # Process the feedback text
50
+ processed_feedback = process_feedback(feedback_text)
51
+
52
+ #print ('processed', processed_feedback)
53
+ # Example result format for each file
54
+ file_result = {
55
+ "summary": processed_feedback['summary'],
56
+ "areas_of_improvement": processed_feedback['areas_of_improvement'],
57
+ "actionable_objectives": processed_feedback['actionable_objectives'],
58
+ "sentiment": processed_feedback['sentiment'],
59
+ "themes": processed_feedback['themes']
60
+ }
61
+
62
+ #print(file_result)
63
+ #print (type(file_result))
64
+
65
+ return feedback_text, file_result
66
+
67
+
68
+ def process_feedback(feedback_text):
69
+ # Example processing steps
70
+ # You need to replace these with your actual feedback analysis logic
71
+ #print (feedback_text)
72
+ summary = analyze_feedback("Provide the summary of the feedback first and then after the summary, analyze and list down the top achievements in this Feedback and pull out any key themes from the Feedback: " + feedback_text)
73
+
74
+ # Simple sentiment analysis
75
+ sentiment = analyze_feedback("What are the overall sentiment score and reasons for the score in this feedback, if 1 is negative and 10 is rated as positive. Provide responses as colon delimeted format: Who gave the Feedback:What was the sentiment score:why was the score given for this feedback: " + feedback_text)
76
+
77
+ # Theme identification (implement your logic)
78
+ themes = analyze_feedback("What are the key recurring positive and negative themes in this feedback: " + feedback_text)
79
+
80
+ #print ("Summ",summary)
81
+ #strengths = analyze_feedback("What are the key strengths in this feedback: " + feedback_text)
82
+ areas_of_improvement = analyze_feedback("Which hard and soft skills may need development according to this Feedback: " + feedback_text)
83
+ actionable_objectives = analyze_feedback("Suggest actionable objectives based on this feedback and steps to accomplish these objectives: " + feedback_text)
84
+
85
+ # This depends on how the response is formatted. You may need to adjust the parsing logic
86
+ sentiment_data = [] # This will be a list of dictionaries
87
+ print (sentiment)
88
+ # Example of processing (you'll need to adjust this based on actual response format)
89
+ for line in sentiment.split('\n'):
90
+ if line.strip():
91
+ parts = line.split(':')
92
+ if len(parts) >= 3:
93
+ sentiment_data.append({
94
+ "Giver": parts[0].strip(),
95
+ "Score": parts[1].strip(),
96
+ "Reason": parts[2].strip()
97
+ })
98
+ # Assign the results to analysis_results
99
+ analysis_results = {
100
+ "summary": summary,
101
+ "themes": themes,
102
+ "areas_of_improvement": areas_of_improvement,
103
+ "actionable_objectives": actionable_objectives,
104
+ "sentiment": sentiment_data
105
+ }
106
+ #print (analysis_results)
107
+
108
+ return analysis_results
109
+
110
+ def display_results(uploaded_files):
111
+ """
112
+ Processes uploaded feedback files and displays the results.
113
+
114
+ Args:
115
+ uploaded_files (list): A list of file paths to uploaded feedback files.
116
+
117
+ Returns:
118
+ tuple: A tuple containing various analysis results or an empty string if no files were uploaded.
119
+ """
120
+ feedback, results = process_files(uploaded_files)
121
+ if results:
122
+ # Extract each area of feedback from the dictionary
123
+ summary = results['summary'] if 'summary' in results else ""
124
+ areas_of_improvement = results['areas_of_improvement'] if 'areas_of_improvement' in results else ""
125
+ actionable_objectives = results['actionable_objectives'] if 'actionable_objectives' in results else ""
126
+ themes_text = results['themes'] if 'themes' in results else ""
127
+ #sentiments_text = results['sentiment'] if 'sentiment' in results else ""
128
+ sentiment_table=[]
129
+ if results:
130
+ for sentiment_entry in results['sentiment']:
131
+ entry_as_list = [sentiment_entry['Giver'], sentiment_entry['Score'], sentiment_entry['Reason']]
132
+ sentiment_table.append(entry_as_list)
133
+
134
+ # Create a Pandas DataFrame
135
+ df = pd.DataFrame(sentiment_table, columns=["Giver", "Score", "Reason"])
136
+ df = df.iloc[1:]
137
+ return summary, themes_text, areas_of_improvement, actionable_objectives,df
138
+ else:
139
+ return "","","","",[], None
140
+
141
+ def image_to_base64(image_path):
142
+ """
143
+ Reads an image file and returns its base64 encoded representation.
144
+
145
+ Args:
146
+ image_path (str): The path to the image file.
147
+
148
+ Returns:
149
+ str: The base64 encoded representation of the image data.
150
+ """
151
+
152
+ with open(image_path, "rb") as image_file:
153
+ return base64.b64encode(image_file.read()).decode("utf-8")
154
+
155
+
156
+ # Encode the logo image into base64
157
+ logo_base64 = image_to_base64("pixelpk_logo.png")
158
+
159
+
160
+ markdown_content = f"""
161
+ <img src="data:image/png;base64,{logo_base64}" alt="Feedback Logo" style="width: 100px; height: 100px; margin-top: 10px;" />
162
+ <p style="margin-top: 5px;">Feedback analyzer give employee/students feedback, providing key insights into strengths, areas for improvement, and overall sentiments.</p>
163
+ """
164
+
165
+ # Custom CSS for styling the interface
166
+ custom_css = """
167
+ <style>
168
+ .gradio-container {
169
+ font-family: 'Helvetica Neue', Arial, sans-serif;
170
+ }
171
+ .gradio-accordion {
172
+ border-radius: 5px;
173
+ }
174
+ .gradio-accordion .gradio-tab {
175
+ padding: 10px;
176
+ }
177
+ .gradio-textbox {
178
+ height: auto;
179
+ min-height: 50px;
180
+ max-height: 300px; /* Adjust as needed */
181
+ overflow-y: auto; /* Enables vertical scrolling */
182
+ }
183
+ </style>
184
+ """
185
+
186
+
187
+ with gr.Blocks(gr.themes.Monochrome(), css=custom_css) as demo:
188
+ # Display introductory markdown content
189
+ gr.Markdown(f"<center>{markdown_content}</center>")
190
+ # Layout elements in a row
191
+ with gr.Column():
192
+
193
+ # Upload button for selecting files
194
+ upload_button = gr.File(file_types=["txt"], file_count="multiple")
195
+ # Button to trigger analysis
196
+ submit_button = gr.Button("Submit")
197
+
198
+ # Accordion to display analysis results
199
+ with gr.Accordion("Analysis Results"):
200
+ # Tab for summary information
201
+ with gr.Tab("Summary"):
202
+ output_summary = gr.Textbox(label="Summary", lines = 4, placeholder="Summary will appear here...")
203
+ # Tab for key insights
204
+ with gr.Tab("Key Insights"):
205
+ output_improvement_areas = gr.Textbox(label="Key Insights", lines = 15, placeholder="Themes will appear here...")
206
+ # Tab for specific improvement areas
207
+ with gr.Tab("Improvement Areas"):
208
+ output_themes = gr.Textbox(label="Analysis", lines = 15, placeholder="Analysis will appear here...")
209
+ # Tab for actionable objectives
210
+ with gr.Tab("Objectives"):
211
+ output_actionable_objectives = gr.Textbox(label="Actionable Objectives", lines = 15, placeholder="Objectives will appear here...")
212
+ # Tab for sentiment analysis results
213
+ with gr.Tab("Sentiments"):
214
+ output_sentiment_table = gr.Dataframe(label="Sediment Analysis")
215
+
216
+ # Connect button click to analysis function
217
+ submit_button.click(
218
+ display_results,
219
+ inputs = [upload_button],
220
+ outputs = [
221
+ output_summary,
222
+ output_themes,
223
+ output_improvement_areas,
224
+ output_actionable_objectives,
225
+ output_sentiment_table
226
+ ]
227
+ )
228
+
229
+ # Launch the Gradio interface
230
+ demo.launch(share = True)
231
+