maria355 commited on
Commit
76ae88d
·
verified ·
1 Parent(s): c43e340

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +197 -4
app.py CHANGED
@@ -1124,6 +1124,201 @@ def generate_hierarchical_flowchart(vector_store, question):
1124
  except Exception as e:
1125
  print(f"Error generating flowchart: {str(e)}")
1126
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1127
  # Main Navigation Menu using native Streamlit components
1128
  tabs = st.tabs(["📄 Upload", "🔍 Q&A", "📝 Summarize", "📊 Analyze"])
1129
 
@@ -1536,9 +1731,8 @@ with tabs[3]:
1536
  "Technical Complexity Assessment",
1537
  "Key Definitions Extraction",
1538
  "Action Items Identification"
1539
- ]
1540
-
1541
- selected_analysis = st.selectbox("Select analysis type:", custom_analysis_types)
1542
 
1543
 
1544
  custom_learning_styles = ["Standard", "Visual learner", "Auditory learner", "Reading/writing learner", "Kinesthetic learner"]
@@ -1787,4 +1981,3 @@ with st.sidebar.expander("🔗 Connect With Me", expanded=False):
1787
  </div>
1788
  <hr>
1789
  """, unsafe_allow_html=True)
1790
-
 
1124
  except Exception as e:
1125
  print(f"Error generating flowchart: {str(e)}")
1126
  return None
1127
+ def perform_custom_analysis(analysis_type, vector_store, learning_style="Standard"):
1128
+ """
1129
+ Perform custom analysis based on the selected analysis type
1130
+
1131
+ Args:
1132
+ analysis_type (str): Type of analysis to perform
1133
+ vector_store: The vector store containing documents
1134
+ learning_style (str): Learning style for formatting the response
1135
+
1136
+ Returns:
1137
+ dict: Analysis results with formatted output
1138
+ """
1139
+
1140
+ if not vector_store:
1141
+ return {"result": "No documents available for analysis. Please upload documents first.", "flowchart": None}
1142
+
1143
+ try:
1144
+ # Initialize LLM
1145
+ llm = initialize_llm()
1146
+ if not llm:
1147
+ return {"result": "Failed to initialize language model.", "flowchart": None}
1148
+
1149
+ # Get learning style instructions
1150
+ learning_style_instructions = get_learning_style_prompt(learning_style)
1151
+
1152
+ # Add specific instructions for visual elements when visual learner is selected
1153
+ visual_instructions = ""
1154
+ if learning_style == "Visual learner":
1155
+ visual_instructions = """
1156
+ You MUST include these visual elements in your response:
1157
+ 1. A table summarizing key points
1158
+ 2. Use of spatial organization for information
1159
+ 3. Clear headings and structured format
1160
+
1161
+ Do not mention that you're including these elements because of instructions - just include them naturally.
1162
+ """
1163
+
1164
+ # Get relevant documents
1165
+ docs = vector_store.similarity_search("", k=10)
1166
+ combined_text = "\n\n".join([doc.page_content for doc in docs])
1167
+
1168
+ # Define different analysis types and their prompts
1169
+ analysis_prompts = {
1170
+ "Key Themes Analysis": """
1171
+ Analyze the following documents and identify the main themes and topics.
1172
+ For each theme, provide:
1173
+ 1. A clear description of the theme
1174
+ 2. Supporting evidence from the documents
1175
+ 3. The significance or importance of this theme
1176
+
1177
+ Documents:
1178
+ {text}
1179
+
1180
+ {learning_style_instructions}
1181
+ {visual_instructions}
1182
+
1183
+ KEY THEMES ANALYSIS:
1184
+ """,
1185
+
1186
+ "Sentiment Analysis": """
1187
+ Analyze the overall sentiment and tone of the following documents.
1188
+ Provide:
1189
+ 1. Overall sentiment classification (Positive/Negative/Neutral)
1190
+ 2. Key emotional indicators found in the text
1191
+ 3. Examples of language that supports your assessment
1192
+ 4. Any shifts in sentiment throughout the documents
1193
+
1194
+ Documents:
1195
+ {text}
1196
+
1197
+ {learning_style_instructions}
1198
+ {visual_instructions}
1199
+
1200
+ SENTIMENT ANALYSIS:
1201
+ """,
1202
+
1203
+ "Content Structure Analysis": """
1204
+ Analyze the structure and organization of the following documents.
1205
+ Provide:
1206
+ 1. Main sections or topics covered
1207
+ 2. How the information is organized
1208
+ 3. Logical flow and connections between ideas
1209
+ 4. Any gaps or areas that could be better developed
1210
+
1211
+ Documents:
1212
+ {text}
1213
+
1214
+ {learning_style_instructions}
1215
+ {visual_instructions}
1216
+
1217
+ CONTENT STRUCTURE ANALYSIS:
1218
+ """,
1219
+
1220
+ "Key Insights Extraction": """
1221
+ Extract the most important insights and takeaways from the following documents.
1222
+ For each insight, provide:
1223
+ 1. The main insight or finding
1224
+ 2. Supporting evidence or context
1225
+ 3. Potential implications or applications
1226
+ 4. Why this insight is significant
1227
+
1228
+ Documents:
1229
+ {text}
1230
+
1231
+ {learning_style_instructions}
1232
+ {visual_instructions}
1233
+
1234
+ KEY INSIGHTS EXTRACTION:
1235
+ """,
1236
+
1237
+ "Comparative Analysis": """
1238
+ Perform a comparative analysis of the different topics, viewpoints, or sections in the documents.
1239
+ Provide:
1240
+ 1. Main points of comparison
1241
+ 2. Similarities and differences
1242
+ 3. Strengths and weaknesses of different approaches
1243
+ 4. Overall assessment and recommendations
1244
+
1245
+ Documents:
1246
+ {text}
1247
+
1248
+ {learning_style_instructions}
1249
+ {visual_instructions}
1250
+
1251
+ COMPARATIVE ANALYSIS:
1252
+ """,
1253
+
1254
+ "Question Generation": """
1255
+ Based on the following documents, generate thoughtful questions that would help someone understand the content better.
1256
+ Create:
1257
+ 1. 5-7 comprehension questions about the main content
1258
+ 2. 3-5 analytical questions that require deeper thinking
1259
+ 3. 2-3 application questions about how to use this information
1260
+ 4. Brief explanations of why these questions are important
1261
+
1262
+ Documents:
1263
+ {text}
1264
+
1265
+ {learning_style_instructions}
1266
+ {visual_instructions}
1267
+
1268
+ QUESTION GENERATION:
1269
+ """
1270
+ }
1271
+
1272
+ # Get the appropriate prompt
1273
+ prompt_template = analysis_prompts.get(analysis_type, analysis_prompts["Key Themes Analysis"])
1274
+
1275
+ # Create the prompt
1276
+ prompt = PromptTemplate(
1277
+ input_variables=["text", "learning_style_instructions", "visual_instructions"],
1278
+ template=prompt_template
1279
+ )
1280
+
1281
+ # Create and run the chain
1282
+ chain = LLMChain(llm=llm, prompt=prompt)
1283
+ result = chain.run(
1284
+ text=combined_text,
1285
+ learning_style_instructions=learning_style_instructions,
1286
+ visual_instructions=visual_instructions
1287
+ )
1288
+
1289
+ # Generate flowchart for visual learners
1290
+ flowchart_result = None
1291
+ if learning_style == "Visual learner":
1292
+ try:
1293
+ flowchart_result = generate_hierarchical_flowchart(
1294
+ vector_store,
1295
+ f"Create a visual representation of {analysis_type.lower()}"
1296
+ )
1297
+ except Exception as e:
1298
+ logger.error(f"Error generating flowchart: {str(e)}")
1299
+ flowchart_result = None
1300
+
1301
+ # Extract source information
1302
+ sources = []
1303
+ for doc in docs:
1304
+ if "source" in doc.metadata:
1305
+ if doc.metadata["source"] not in sources:
1306
+ sources.append(doc.metadata["source"])
1307
+
1308
+ return {
1309
+ "result": result,
1310
+ "sources": sources,
1311
+ "flowchart": flowchart_result,
1312
+ "analysis_type": analysis_type
1313
+ }
1314
+
1315
+ except Exception as e:
1316
+ logger.error(f"Error in perform_custom_analysis: {str(e)}")
1317
+ return {
1318
+ "result": f"Error performing analysis: {str(e)}",
1319
+ "flowchart": None,
1320
+ "analysis_type": analysis_type
1321
+ }
1322
  # Main Navigation Menu using native Streamlit components
1323
  tabs = st.tabs(["📄 Upload", "🔍 Q&A", "📝 Summarize", "📊 Analyze"])
1324
 
 
1731
  "Technical Complexity Assessment",
1732
  "Key Definitions Extraction",
1733
  "Action Items Identification"
1734
+ ]
1735
+ selected_analysis = st.selectbox("Select analysis type:", custom_analysis_types)
 
1736
 
1737
 
1738
  custom_learning_styles = ["Standard", "Visual learner", "Auditory learner", "Reading/writing learner", "Kinesthetic learner"]
 
1981
  </div>
1982
  <hr>
1983
  """, unsafe_allow_html=True)