Neom99 commited on
Commit
267dca4
Β·
verified Β·
1 Parent(s): 9eeec3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +370 -79
app.py CHANGED
@@ -1,17 +1,22 @@
1
  import os
2
  import gradio as gr
3
  from dotenv import load_dotenv
 
 
 
4
 
5
  # --- RAG Libraries ---
6
- # Removed: PyPDFLoader, TextLoader, Docx2txtLoader
7
  from langchain.text_splitter import RecursiveCharacterTextSplitter
8
- from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI # Assuming you are using Gemini
9
  from langchain.chains import RetrievalQA
10
  from langchain.prompts import PromptTemplate
11
- from langchain.schema import Document # Needed for creating custom documents from Excel/CSV
12
 
13
- # For Excel and CSV file handling
14
- import pandas as pd
 
 
 
15
 
16
  # Load environment variables (for API keys when running locally)
17
  load_dotenv()
@@ -19,9 +24,8 @@ load_dotenv()
19
  # --- RAG Setup ---
20
  rag_initialized = False
21
  try:
22
- # --- Use Google Gemini Models ---
23
  embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
24
- llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.7) # Or "gemini-1.5-flash-latest", etc.
25
  rag_initialized = True
26
  print("Google Gemini embeddings and LLM initialized successfully.")
27
  except Exception as e:
@@ -31,11 +35,12 @@ except Exception as e:
31
  print("RAG functionality will not work without a valid Google API key.")
32
 
33
 
34
- def get_document_content(file_path, file_extension, filename):
35
  """
36
- Loads and returns text from Excel or CSV files, converting each row into a Langchain Document object.
 
37
  """
38
- print(f"Attempting to load document: {filename} with extension {file_extension}")
39
  documents = []
40
 
41
  if file_extension in ['.xlsx', '.xls']:
@@ -43,126 +48,412 @@ def get_document_content(file_path, file_extension, filename):
43
  df = pd.read_excel(file_path)
44
  print(f"Excel file '{filename}' loaded with {len(df)} rows.")
45
  except Exception as e:
46
- raise ValueError(f"Could not process Excel file '{filename}': {e}")
47
  elif file_extension == '.csv':
48
  try:
49
  df = pd.read_csv(file_path)
50
  print(f"CSV file '{filename}' loaded with {len(df)} rows.")
51
  except Exception as e:
52
- raise ValueError(f"Could not process CSV file '{filename}': {e}")
53
  else:
54
- # This case should ideally not be reached if Gradio's file_types is correct
55
- raise ValueError(f"Unsupported file type: {file_extension}. Please upload .xlsx, .xls, or .csv files.")
 
 
56
 
57
- # Convert DataFrame rows to text chunks for RAG
58
- # This loop applies to both Excel and CSV dataframes
59
  for index, row in df.iterrows():
60
- # Customize how you want each row to be represented as text for the LLM
61
- # Example: F"Row {index+1}: {row.to_json()}" provides all column data as JSON.
62
- # For more readable context, you might pick specific columns or format:
63
- # doc_content = f"Row {index+1} from '{filename}': " + ", ".join([f"{col}: {val}" for col, val in row.items() if pd.notna(val)])
64
- doc_content = f"Row {index+1} from '{filename}': {row.to_json()}"
65
 
66
- documents.append(Document(page_content=doc_content, metadata={"source": filename, "row_number": index+1}))
67
-
68
- if not documents:
69
- raise ValueError("No usable data extracted from the document. It might be empty or improperly formatted.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- print(f"File '{filename}' processed into {len(documents)} Langchain Documents (one per row).")
 
 
 
 
 
72
  return documents
73
 
74
- def analyze_document_with_rag(file, query):
 
 
 
75
  """
76
- Core RAG function that processes the uploaded Excel/CSV file and answers the query.
 
77
  """
 
 
78
  if not rag_initialized:
79
- return "RAG system not initialized. Please ensure your **GOOGLE_API_KEY** is configured correctly in Hugging Face Space secrets."
 
80
 
81
  if file is None:
82
- return "Please upload a file to analyze."
 
 
 
83
  if not query.strip():
84
- return "Please enter a question or analysis request."
85
 
86
  file_path = file.name
87
  filename = os.path.basename(file_path)
88
  file_extension = os.path.splitext(filename)[1].lower()
89
 
90
  try:
91
- # 1. Load Document (Excel/CSV specific)
92
- documents = get_document_content(file_path, file_extension, filename)
93
 
94
- # 2. Split Text (even Excel rows can be split further if they are long)
95
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
96
  texts = text_splitter.split_documents(documents)
97
- print(f"Text split into {len(texts)} chunks for embedding.")
98
-
99
  if not texts:
100
- return "No usable text extracted from the document after splitting. The file might be empty or the data too sparse."
 
101
 
102
- # 3. Create Embeddings and Vector Store
103
- print("Creating embeddings and vector store (FAISS)...")
 
 
 
 
 
104
  vectorstore = FAISS.from_documents(texts, embeddings)
105
- print("Vector store created successfully.")
106
 
107
- # 4. Set up RetrievalQA Chain with Custom Prompt
108
  template = """
109
- You are an expert data analyst. Use the following context to answer the user's question.
110
- The context contains information extracted from rows of an Excel or CSV file.
111
- Each entry in the context represents a row from the original file.
112
- Be thorough, precise, and directly address all parts of the question.
113
- If the question cannot be answered from the provided context, please state that explicitly
114
- and explain why (e.g., "The provided data does not contain information about...").
 
 
 
 
 
 
 
 
 
115
  Do not make up information.
116
 
117
- Context:
118
  {context}
119
 
120
  Question: {question}
121
 
122
- Your detailed analysis and answer:
123
  """
124
  QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
125
 
126
  qa_chain = RetrievalQA.from_chain_type(
127
  llm=llm,
128
  chain_type="stuff",
129
- retriever=vectorstore.as_retriever(),
130
  chain_type_kwargs={"prompt": QA_CHAIN_PROMPT}
131
  )
132
 
133
- # 5. Get Answer
134
- print(f"Running RAG query for: '{query}'")
135
  result = qa_chain.run(query)
136
- print(f"Analysis complete. Result length: {len(result)} characters.")
137
 
138
- return result
 
 
 
 
139
 
140
  except ValueError as e:
141
- print(f"Value error during analysis: {e}")
142
- return f"Analysis Error: {e}"
143
  except Exception as e:
144
- print(f"An unexpected error occurred during analysis: {e}")
145
- return "An internal server error occurred during analysis. Please try again or contact support. Check the Space logs for more details."
146
-
147
-
148
- # --- Gradio Interface Setup ---
149
- iface = gr.Interface(
150
- fn=analyze_document_with_rag,
151
- inputs=[
152
- # Updated file_types to only include Excel and CSV formats
153
- gr.File(label="Upload an Excel or CSV Document", file_types=[".xlsx", ".xls", ".csv"], type="file"),
154
- gr.Textbox(label="Your Question/Analysis Request", placeholder="e.g., 'Summarize the sales data.' or 'What is the total revenue for product X?'")
155
- ],
156
- outputs=gr.Textbox(label="Analysis Output", show_copy_button=True),
157
- title="Excel/CSV Data Analysis with RAG (Powered by Gemini)",
158
- description=(
159
- "Upload an Excel (.xlsx, .xls) or CSV (.csv) file and ask a question to get an AI-powered analysis. "
160
- "Each row of your spreadsheet will be processed as a context unit for the AI."
161
- ),
162
- allow_flagging="never",
163
- theme=gr.themes.Soft(),
164
- examples=[] # Add examples if you have sample Excel/CSV files
165
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
  # This block is for running the Gradio app locally.
168
  if __name__ == "__main__":
@@ -172,4 +463,4 @@ if __name__ == "__main__":
172
  print("RAG functionality will not work locally without it. Please set it in a .env file or your environment.")
173
  print("Example: GOOGLE_API_KEY='your-gemini-api-key-here'")
174
  print("="*80 + "\n")
175
- iface.launch()
 
1
  import os
2
  import gradio as gr
3
  from dotenv import load_dotenv
4
+ import pandas as pd
5
+ from datetime import datetime
6
+ import io # For in-memory file operations
7
 
8
  # --- RAG Libraries ---
 
9
  from langchain.text_splitter import RecursiveCharacterTextSplitter
10
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
11
  from langchain.chains import RetrievalQA
12
  from langchain.prompts import PromptTemplate
13
+ from langchain.schema import Document
14
 
15
+ # --- File Generation Libraries ---
16
+ from fpdf import FPDF # For PDF
17
+ from docx import Document as DocxDocument # For Word
18
+ from docx.shared import Inches # For document formatting in Word
19
+ from docx.enum.text import WD_ALIGN_PARAGRAPH # For text alignment in Word
20
 
21
  # Load environment variables (for API keys when running locally)
22
  load_dotenv()
 
24
  # --- RAG Setup ---
25
  rag_initialized = False
26
  try:
 
27
  embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
28
+ llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.7)
29
  rag_initialized = True
30
  print("Google Gemini embeddings and LLM initialized successfully.")
31
  except Exception as e:
 
35
  print("RAG functionality will not work without a valid Google API key.")
36
 
37
 
38
+ def get_document_content_from_tickets(file_path, file_extension, filename):
39
  """
40
+ Loads and returns text from Excel or CSV files, treating each row as a customer ticket.
41
+ Dynamically reads all columns and includes them in the document content.
42
  """
43
+ print(f"Attempting to load customer ticket data: {filename} with extension {file_extension}")
44
  documents = []
45
 
46
  if file_extension in ['.xlsx', '.xls']:
 
48
  df = pd.read_excel(file_path)
49
  print(f"Excel file '{filename}' loaded with {len(df)} rows.")
50
  except Exception as e:
51
+ raise ValueError(f"Could not process Excel file '{filename}': {e}. Ensure it's a valid Excel format for ticket data.")
52
  elif file_extension == '.csv':
53
  try:
54
  df = pd.read_csv(file_path)
55
  print(f"CSV file '{filename}' loaded with {len(df)} rows.")
56
  except Exception as e:
57
+ raise ValueError(f"Could not process CSV file '{filename}': {e}. Ensure it's a valid CSV format for ticket data.")
58
  else:
59
+ raise ValueError(f"Unsupported file type: {file_extension}. Only .xlsx, .xls, or .csv files are accepted for ticket data.")
60
+
61
+ if df.empty:
62
+ raise ValueError("The uploaded file contains no data or no rows could be parsed.")
63
 
64
+ # Dynamically include all non-empty column data in the document content
 
65
  for index, row in df.iterrows():
66
+ ticket_content_parts = []
67
+ ticket_id = f"Row {index+1}" # Default ticket ID
 
 
 
68
 
69
+ for col_name, value in row.items():
70
+ if pd.notna(value) and str(value).strip() != '': # Check for NaN and empty strings
71
+ formatted_col_name = col_name.replace('_', ' ').title() # Make column names readable
72
+ ticket_content_parts.append(f"{formatted_col_name}: {value}")
73
+
74
+ # Try to capture a good ticket ID or issue description from common columns
75
+ if col_name.lower() in ['ticketid', 'ticket_id', 'id']:
76
+ ticket_id = value
77
+ elif col_name.lower() in ['issue', 'description', 'problem'] and len(str(value)) > 10: # Ensure it's a substantive description
78
+ pass # Issue will be in ticket_content_parts
79
+
80
+ if not ticket_content_parts:
81
+ raise ValueError(f"Row {index+1} from '{filename}' has no detectable content.")
82
+
83
+ ticket_content = "\n".join(ticket_content_parts)
84
+
85
+ # Store all row data in metadata for potential advanced RAG (though not used directly in prompt here)
86
+ metadata = {"source": filename, "ticket_row_number": index+1, "ticket_id": ticket_id}
87
+ metadata.update(row.to_dict()) # Add all columns to metadata
88
 
89
+ documents.append(Document(page_content=ticket_content, metadata=metadata))
90
+
91
+ if not documents:
92
+ raise ValueError("No usable ticket data extracted from the document.")
93
+
94
+ print(f"File '{filename}' processed into {len(documents)} Langchain Documents (one per ticket).")
95
  return documents
96
 
97
+ # Global variable to store the last analysis result for download
98
+ last_analysis_result_text = ""
99
+
100
+ def perform_ticket_analysis(file, query):
101
  """
102
+ Main function to analyze customer tickets using RAG.
103
+ Updates a global variable with the result for download functions.
104
  """
105
+ global last_analysis_result_text # Declare global to modify it
106
+
107
  if not rag_initialized:
108
+ last_analysis_result_text = "RAG system not initialized. Please ensure your **GOOGLE_API_KEY** is configured correctly in Hugging Face Space secrets."
109
+ return last_analysis_result_text
110
 
111
  if file is None:
112
+ last_analysis_result_text = "Please upload a customer ticket file."
113
+ return last_analysis_result_text
114
+
115
+ # Default comprehensive analysis if query is empty
116
  if not query.strip():
117
+ query = "Perform a comprehensive analysis of the tickets. Provide categorization, sub-categories, count of issues, detailed description of issues, infer the root cause, suggest potential resolution steps, identify future prediction, and propose proactive measures."
118
 
119
  file_path = file.name
120
  filename = os.path.basename(file_path)
121
  file_extension = os.path.splitext(filename)[1].lower()
122
 
123
  try:
124
+ documents = get_document_content_from_tickets(file_path, file_extension, filename)
 
125
 
 
126
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
127
  texts = text_splitter.split_documents(documents)
128
+
 
129
  if not texts:
130
+ last_analysis_result_text = "No usable text extracted from the ticket data after splitting."
131
+ return last_analysis_result_text
132
 
133
+ # Using FAISS for vector store (ensure it's imported or handled)
134
+ try:
135
+ from langchain_community.vectorstores import FAISS # Import FAISS if not already
136
+ except ImportError:
137
+ # Fallback for older langchain versions or if FAISS not in langchain_community
138
+ from langchain.vectorstores import FAISS # Fallback
139
+
140
  vectorstore = FAISS.from_documents(texts, embeddings)
 
141
 
 
142
  template = """
143
+ You are an expert customer support ticket analyst. Your goal is to provide a comprehensive analysis of customer tickets
144
+ based *only* on the provided context. The context contains individual customer tickets, each representing a row
145
+ from a data file. Each row contains various columns and their values.
146
+
147
+ Analyze the tickets to address the user's query. If the query asks for a general analysis,
148
+ provide the following structured insights. If a specific detail (like root cause) cannot be inferred from the available ticket data,
149
+ state 'Undetermined' or 'N/A'.
150
+
151
+ **Desired Output Format (Markdown Table):**
152
+ | Category | Sub-Category | Count | Issue Description (Summary) | Root Cause (Inferred) | Potential Resolution Steps | Future Prediction | Proactive Measures |
153
+ |---|---|---|---|---|---|---|---|
154
+ | ... | ... | ... | ... | ... | ... | ... | ... |
155
+
156
+ If the query is specific and doesn't require all columns, answer directly.
157
+ If the information is not present in the provided tickets, state that clearly.
158
  Do not make up information.
159
 
160
+ Context (Customer Tickets):
161
  {context}
162
 
163
  Question: {question}
164
 
165
+ Your detailed ticket analysis:
166
  """
167
  QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
168
 
169
  qa_chain = RetrievalQA.from_chain_type(
170
  llm=llm,
171
  chain_type="stuff",
172
+ retriever=vectorstore.as_retriever(k=10), # Retrieve more chunks for better context
173
  chain_type_kwargs={"prompt": QA_CHAIN_PROMPT}
174
  )
175
 
 
 
176
  result = qa_chain.run(query)
 
177
 
178
+ disclaimer = "\n\n**Note:** 'Root Cause', 'Future Prediction', and 'Proactive Measures' are AI-inferred based on descriptions provided. For critical decisions, human expert review is essential."
179
+ final_result = result + disclaimer
180
+
181
+ last_analysis_result_text = final_result # Store the result
182
+ return final_result
183
 
184
  except ValueError as e:
185
+ last_analysis_result_text = f"Ticket Analysis Error: {e}"
186
+ return last_analysis_result_text
187
  except Exception as e:
188
+ last_analysis_result_text = f"An unexpected error occurred during analysis: {e}. Check logs for details."
189
+ print(f"Error: {e}")
190
+ return last_analysis_result_text
191
+
192
+ # --- Output Download Functions ---
193
+
194
+ def generate_pdf_report(markdown_text):
195
+ if not markdown_text or "RAG system not initialized" in markdown_text or "Error:" in markdown_text:
196
+ return None # Return None if no valid text to generate
197
+
198
+ pdf = FPDF()
199
+ pdf.add_page()
200
+ pdf.set_font("Arial", size=12)
201
+
202
+ # Add a title
203
+ pdf.set_font("Arial", 'B', 16)
204
+ pdf.cell(0, 10, "AI Customer Ticket Analysis Report", ln=True, align='C')
205
+ pdf.ln(5) # Line break
206
+
207
+ # Add timestamp
208
+ pdf.set_font("Arial", size=10)
209
+ pdf.cell(0, 10, f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='R')
210
+ pdf.ln(5)
211
+
212
+ pdf.set_font("Arial", size=12)
213
+ # Simple parsing for markdown: bold, italics, paragraphs
214
+ # This won't perfectly render markdown tables, but will output text.
215
+ # For full markdown rendering, you'd need a more advanced library or HTML conversion.
216
+ lines = markdown_text.split('\n')
217
+ for line in lines:
218
+ line = line.replace('**', '').replace('*', '') # Remove bold/italics for simple text
219
+ if line.strip().startswith('|') and '|---' not in line: # Basic table line detection
220
+ pdf.multi_cell(0, 7, line)
221
+ elif line.strip() == '':
222
+ pdf.ln(5) # Add a larger line break for empty lines
223
+ else:
224
+ pdf.multi_cell(0, 7, line) # Use multi_cell for wrapping text
225
+
226
+ # Save to an in-memory buffer
227
+ pdf_output = io.BytesIO()
228
+ pdf.output(pdf_output)
229
+ pdf_output.seek(0)
230
+
231
+ file_name = f"ticket_analysis_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
232
+ # Gradio expects a path, so we'll save it temporarily and return the path
233
+ temp_dir = os.path.join(os.getcwd(), "temp_files")
234
+ os.makedirs(temp_dir, exist_ok=True)
235
+ temp_file_path = os.path.join(temp_dir, file_name)
236
+ with open(temp_file_path, "wb") as f:
237
+ f.write(pdf_output.read())
238
+ return temp_file_path
239
+
240
+
241
+ def generate_docx_report(markdown_text):
242
+ if not markdown_text or "RAG system not initialized" in markdown_text or "Error:" in markdown_text:
243
+ return None # Return None if no valid text to generate
244
+
245
+ doc = DocxDocument()
246
+
247
+ # Add a title
248
+ doc.add_heading("AI Customer Ticket Analysis Report", level=1)
249
+ doc.add_paragraph(f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", style='Intense Quote')
250
+ doc.add_paragraph("") # Blank line
251
+
252
+ # Add content
253
+ lines = markdown_text.split('\n')
254
+ is_in_table = False
255
+ table = None
256
+ header_row = []
257
+
258
+ for line in lines:
259
+ stripped_line = line.strip()
260
+
261
+ if stripped_line.startswith('|') and '|---' not in stripped_line:
262
+ # It's a table row
263
+ cells_data = [cell.strip() for cell in stripped_line.strip('|').split('|')]
264
+
265
+ if not is_in_table: # First row of a table
266
+ is_in_table = True
267
+ table = doc.add_table(rows=1, cols=len(cells_data))
268
+ table.style = 'Table Grid' # Apply a table style
269
+ hdr_cells = table.rows[0].cells
270
+ for i, text in enumerate(cells_data):
271
+ hdr_cells[i].text = text
272
+ header_row.append(text) # Store header for reference
273
+ else: # Subsequent row in table
274
+ if len(cells_data) == len(header_row): # Ensure consistent columns
275
+ row_cells = table.add_row().cells
276
+ for i, text in enumerate(cells_data):
277
+ row_cells[i].text = text
278
+ else: # Fallback if table structure is inconsistent, add as paragraph
279
+ doc.add_paragraph(line)
280
+ elif '|---' in stripped_line: # Table separator line
281
+ continue # Skip this line
282
+ else:
283
+ if is_in_table: # End of table
284
+ is_in_table = False
285
+ table = None
286
+ header_row = []
287
+ doc.add_paragraph("") # Add a space after table
288
+
289
+ # Regular paragraph
290
+ if stripped_line.startswith('**') and stripped_line.endswith('**'):
291
+ p = doc.add_paragraph()
292
+ p.add_run(stripped_line.strip('**')).bold = True
293
+ elif stripped_line.startswith('*') or stripped_line.startswith('-'):
294
+ # Basic list item
295
+ doc.add_paragraph(stripped_line, style='List Bullet')
296
+ else:
297
+ doc.add_paragraph(stripped_line)
298
+
299
+ # Save to an in-memory buffer
300
+ docx_output = io.BytesIO()
301
+ doc.save(docx_output)
302
+ docx_output.seek(0)
303
+
304
+ file_name = f"ticket_analysis_summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
305
+ temp_dir = os.path.join(os.getcwd(), "temp_files")
306
+ os.makedirs(temp_dir, exist_ok=True)
307
+ temp_file_path = os.path.join(temp_dir, file_name)
308
+ with open(temp_file_path, "wb") as f:
309
+ f.write(docx_output.read())
310
+ return temp_file_path
311
+
312
+
313
+ # --- Gradio Interface Setup (using gr.Blocks for complex layout) ---
314
+
315
+ # --- Sample Data (same as previous response, ensure these files are uploaded to your space) ---
316
+ # 'sample_customer_tickets_minimal.csv'
317
+ # 'sample_customer_tickets_full.csv'
318
+ example_data = [
319
+ ["sample_customer_tickets_minimal.csv", "Provide a comprehensive analysis of the tickets, focusing on common issues."],
320
+ ["sample_customer_tickets_full.csv", "Summarize high-priority open issues, their root causes, and suggest proactive measures."],
321
+ ["sample_customer_tickets_full.csv", "Categorize all tickets by their primary issue type and count them."],
322
+ ]
323
+
324
+ with gr.Blocks(theme=gr.themes.Soft(), css="""
325
+ .gradio-container { max-width: 900px; margin: auto; padding: 20px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); border-radius: 10px; }
326
+ h1, h2, h3, h4, h5, h6 { color: #3F51B5; }
327
+ p { line-height: 1.6; }
328
+ .gr-button { background-color: #4CAF50; color: white; border-radius: 5px; }
329
+ .gr-textarea, .gr-file { border-radius: 5px; border: 1px solid #ddd; }
330
+ .gradio-container .gr-button:hover { background-color: #45a049; }
331
+ #download-buttons { display: flex; justify-content: center; gap: 10px; margin-top: 15px; }
332
+ .download-button { flex-grow: 1; max-width: 200px; }
333
+ """) as demo:
334
+ gr.HTML("""
335
+ <h1 align="center">πŸ•΅οΈβ€β™€οΈ Advanced AI Customer Ticket Analyst πŸ•΅οΈβ€β™€οΈ</h1>
336
+ <p align="center">Unlock deep insights from your customer support data with Gemini RAG!</p>
337
+ """)
338
+
339
+ gr.Markdown(
340
+ """
341
+ ### Welcome to the Advanced AI Customer Ticket Analyst! πŸ‘‹
342
+ This powerful tool leverages **Retrieval-Augmented Generation (RAG)** with **Google Gemini** to provide detailed, structured insights from your customer support tickets.
343
+ Upload your Excel or CSV file and ask a specific question, or leave the query blank for a **comprehensive analysis** including categorization, root causes, predictions, and proactive measures.
344
+
345
+ ---
346
+
347
+ #### **<span style="color: #FF9800;">πŸ’‘ Important Note on AI Inferences:</span>**
348
+ The 'Root Cause', 'Future Prediction', and 'Proactive Measures' are **AI-inferred** based on the descriptions provided in your tickets. These are educated guesses by the AI and should be used for **guidance only**. For critical business decisions, human expert validation is highly recommended.
349
+
350
+ ---
351
+
352
+ #### **<span style="color: #4CAF50;">🟒 Input File Prerequisites & Guidance:</span>**
353
+ * **Supported Formats:** Only `.xlsx` (Excel), `.xls` (Legacy Excel), and `.csv` (Comma Separated Values) files are accepted.
354
+ * **Data Structure:** Your file must contain **tabular data with clear headers in the first row**. Each row is processed as an individual customer ticket.
355
+ * **Dynamic Column Reading:** The tool will attempt to read **ALL** columns present in your file. Ensure column headers are clear and meaningful.
356
+ * **Minimum Columns:** A **minimum of two columns are highly recommended**: one for a unique `Ticket ID` (or `ID`) and one for the `Issue` (or `Description`).
357
+ * **Recommended Columns:** For richer analysis, include columns like `Status`, `Priority`, `CustomerName`, `Timestamp`, etc. The more relevant details provided, the better the AI's inferences.
358
+ * **Size Recommendation:** For optimal performance on the free tier, we recommend files with up to **~1000 rows**. Larger files might experience slower processing or hit API rate limits.
359
+ * **Privacy:** **DO NOT upload sensitive or confidential customer information.** This is a publicly hosted demo.
360
+
361
+ ---
362
+
363
+ #### **<span style="color: #2196F3;">πŸ”΅ Expected Analysis Output:</span>**
364
+ The AI will provide a **clear, actionable, and data-driven answer** based *only* on the information present in your uploaded ticket file.
365
+ If you ask for a comprehensive analysis (or leave the query blank), the output will strive to be in a **Markdown Table format** with the following columns:
366
+ * **Category:** Broad classification (e.g., Network, Software, Billing).
367
+ * **Sub-Category:** More specific classification (e.g., Connectivity, Installation, Invoice Inquiry).
368
+ * **Count:** Number of tickets falling into this category/sub-category (if applicable to query).
369
+ * **Issue Description (Summary):** A concise summary of the issue(s).
370
+ * **Root Cause (Inferred):** The most likely underlying reason for the issue.
371
+ * **Potential Resolution Steps:** Actionable steps to resolve the issue.
372
+ * **Future Prediction:** What might happen if issues persist or aren't addressed.
373
+ * **Proactive Measures:** Steps to prevent similar issues in the future.
374
+
375
+ **<span style="color: #FF9800;">πŸ’‘ Sample Output Structure (from a comprehensive analysis):</span>**
376
+ ```markdown
377
+ | Category | Sub-Category | Count | Issue Description (Summary) | Root Cause (Inferred) | Potential Resolution Steps | Future Prediction | Proactive Measures |
378
+ |-------------|--------------|-------|---------------------------------------------------|---------------------------------|--------------------------------------|-------------------------------------------|--------------------------------------|
379
+ | Network | Connectivity | 1 | Intermittent internet drops, not working. | Faulty router/ISP fluctuations | Router reboot, check ISP status | Increased customer frustration | Regular network diagnostics |
380
+ | Software | Installation | 1 | Software failed to install on startup. | Corrupt installer/conflicting app | Reinstall, check system logs | System instability, productivity loss | Pre-install checks, compatibility tests |
381
+ | Account | Billing | 1 | Customer inquiring about billing statement. | Unclear invoice/customer confusion | Explain statement, provide breakdown | Continued customer churn | Clearer billing statements, FAQs |
382
+ | Credentials | Email Access | 1 | Email access denied after password reset. | Sync issues/account lockouts | Manual account reset, guide user | Loss of critical communication | Automated account sync, better alerts |
383
+ | Hardware | Peripherals | 1 | Printer not responding to print commands. | Driver issue/connection problem | Reinstall driver, check connections | Reduced productivity, equipment downtime | Driver auto-updates, robust setup guide |
384
+ ```
385
+ """
386
+ )
387
+
388
+ with gr.Row():
389
+ file_input = gr.File(
390
+ label="Upload Customer Ticket Data (Excel or CSV)",
391
+ file_types=[".xlsx", ".xls", ".csv"],
392
+ type="filepath", # Use 'filepath' for temp file access
393
+ file_count="single",
394
+ interactive=True
395
+ )
396
+ query_input = gr.Textbox(
397
+ label="Your Analysis Request (Optional, leave blank for full analysis)",
398
+ placeholder="e.g., 'Summarize all high-priority issues' OR leave blank for a full analysis.",
399
+ lines=2,
400
+ interactive=True
401
+ )
402
+
403
+ submit_btn = gr.Button("Analyze Tickets", variant="primary")
404
+
405
+ output_text = gr.Textbox(
406
+ label="AI Ticket Analysis Result",
407
+ show_copy_button=True,
408
+ lines=15, # Increased lines for more comprehensive output
409
+ interactive=False,
410
+ )
411
+
412
+ with gr.Group(elem_id="download-buttons"): # Group buttons for styling
413
+ pdf_btn = gr.Button("Download as PDF Report", elem_classes="download-button")
414
+ docx_btn = gr.Button("Download as Word Document", elem_classes="download-button")
415
+
416
+ pdf_output_file = gr.File(label="Download PDF", visible=False)
417
+ docx_output_file = gr.File(label="Download Word Doc", visible=False)
418
+
419
+ # Event Handlers
420
+ submit_btn.click(
421
+ fn=perform_ticket_analysis,
422
+ inputs=[file_input, query_input],
423
+ outputs=output_text
424
+ )
425
+
426
+ pdf_btn.click(
427
+ fn=generate_pdf_report,
428
+ inputs=output_text, # Pass the content of the output_text box
429
+ outputs=pdf_output_file,
430
+ # After generating, show the download link
431
+ api_name="download_pdf" # Expose for potential external use
432
+ ).then(
433
+ fn=lambda: gr.File(visible=True), # Make the file download component visible
434
+ inputs=[],
435
+ outputs=pdf_output_file
436
+ )
437
+
438
+ docx_btn.click(
439
+ fn=generate_docx_report,
440
+ inputs=output_text, # Pass the content of the output_text box
441
+ outputs=docx_output_file,
442
+ api_name="download_docx" # Expose for potential external use
443
+ ).then(
444
+ fn=lambda: gr.File(visible=True), # Make the file download component visible
445
+ inputs=[],
446
+ outputs=docx_output_file
447
+ )
448
+
449
+ gr.Examples(
450
+ examples=example_data,
451
+ inputs=[file_input, query_input],
452
+ outputs=output_text, # Ensure examples populate the output text box too
453
+ fn=perform_ticket_analysis, # Call the analysis function when example is clicked
454
+ cache_examples=True, # Cache results for faster example loading
455
+ )
456
+
457
 
458
  # This block is for running the Gradio app locally.
459
  if __name__ == "__main__":
 
463
  print("RAG functionality will not work locally without it. Please set it in a .env file or your environment.")
464
  print("Example: GOOGLE_API_KEY='your-gemini-api-key-here'")
465
  print("="*80 + "\n")
466
+ demo.launch()