Update app.py
Browse files
app.py
CHANGED
|
@@ -8,12 +8,18 @@ import re # For regex to clean markdown
|
|
| 8 |
|
| 9 |
# --- RAG Libraries ---
|
| 10 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 11 |
-
from langchain_google_genai import GoogleGenerativeAIEmbeddings,
|
| 12 |
from langchain.chains import RetrievalQA
|
| 13 |
from langchain.prompts import PromptTemplate
|
| 14 |
from langchain.schema import Document
|
| 15 |
from langchain_community.vectorstores import FAISS # Explicitly import FAISS
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Load environment variables (for API keys when running locally)
|
| 18 |
load_dotenv()
|
| 19 |
|
|
@@ -22,7 +28,7 @@ rag_initialized = False
|
|
| 22 |
llm = None # Initialize llm to None
|
| 23 |
embeddings = None # Initialize embeddings to None
|
| 24 |
|
| 25 |
-
# Attempt to get API key from environment BEFORE trying to initialize
|
| 26 |
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 27 |
|
| 28 |
if not google_api_key:
|
|
@@ -36,7 +42,7 @@ else:
|
|
| 36 |
llm_model_id = "gemini-1.5-flash" # The intended model ID
|
| 37 |
|
| 38 |
# Explicitly pass the API key to ChatGoogleGenerativeAI
|
| 39 |
-
llm =
|
| 40 |
|
| 41 |
# Explicitly pass the API key to GoogleGenerativeAIEmbeddings
|
| 42 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=google_api_key)
|
|
@@ -381,7 +387,7 @@ def perform_ticket_analysis(file, query):
|
|
| 381 |
summarized_analysis_html = f"""
|
| 382 |
<div style="margin-bottom: 20px;">
|
| 383 |
<p style="color: #D32F2F;">A parsing error occurred while attempting to transform AI output into a visual summary: {e}</p>
|
| 384 |
-
<p style="color: #D32F2F;
|
| 385 |
<pre style="background-color: #EEE; padding: 15px; border: 1px solid #DDD; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;">{raw_rag_output}</pre>
|
| 386 |
</div>
|
| 387 |
"""
|
|
@@ -476,7 +482,7 @@ def generate_docx_report(): # No input needed as it uses global_raw_markdown_for
|
|
| 476 |
if not markdown_text or "RAG system not initialized" in markdown_text or "Error:" in markdown_text:
|
| 477 |
return None # Return None if no valid text to generate
|
| 478 |
|
| 479 |
-
doc =
|
| 480 |
|
| 481 |
# Add a title
|
| 482 |
doc.add_heading("AI Customer Ticket Analysis Report", level=1)
|
|
|
|
| 8 |
|
| 9 |
# --- RAG Libraries ---
|
| 10 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 11 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenAI
|
| 12 |
from langchain.chains import RetrievalQA
|
| 13 |
from langchain.prompts import PromptTemplate
|
| 14 |
from langchain.schema import Document
|
| 15 |
from langchain_community.vectorstores import FAISS # Explicitly import FAISS
|
| 16 |
|
| 17 |
+
# --- File Generation Libraries ---
|
| 18 |
+
from fpdf import FPDF # For PDF
|
| 19 |
+
from docx import Document # Changed: Import Document directly
|
| 20 |
+
from docx.shared import Inches # For document formatting in Word
|
| 21 |
+
from docx.enum.text import WD_ALIGN_PARAGRAPH # For text alignment in Word
|
| 22 |
+
|
| 23 |
# Load environment variables (for API keys when running locally)
|
| 24 |
load_dotenv()
|
| 25 |
|
|
|
|
| 28 |
llm = None # Initialize llm to None
|
| 29 |
embeddings = None # Initialize embeddings to None
|
| 30 |
|
| 31 |
+
# Attempt to get API key from environment BEFORE trying to initialize
|
| 32 |
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 33 |
|
| 34 |
if not google_api_key:
|
|
|
|
| 42 |
llm_model_id = "gemini-1.5-flash" # The intended model ID
|
| 43 |
|
| 44 |
# Explicitly pass the API key to ChatGoogleGenerativeAI
|
| 45 |
+
llm = ChatGoogleGenAI(model=llm_model_id, temperature=0.7, google_api_key=google_api_key)
|
| 46 |
|
| 47 |
# Explicitly pass the API key to GoogleGenerativeAIEmbeddings
|
| 48 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=google_api_key)
|
|
|
|
| 387 |
summarized_analysis_html = f"""
|
| 388 |
<div style="margin-bottom: 20px;">
|
| 389 |
<p style="color: #D32F2F;">A parsing error occurred while attempting to transform AI output into a visual summary: {e}</p>
|
| 390 |
+
<p style="color: #D32F2F;'>Here is the raw AI output (could not be structured):</p>
|
| 391 |
<pre style="background-color: #EEE; padding: 15px; border: 1px solid #DDD; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;">{raw_rag_output}</pre>
|
| 392 |
</div>
|
| 393 |
"""
|
|
|
|
| 482 |
if not markdown_text or "RAG system not initialized" in markdown_text or "Error:" in markdown_text:
|
| 483 |
return None # Return None if no valid text to generate
|
| 484 |
|
| 485 |
+
doc = Document() # Changed: Use Document directly
|
| 486 |
|
| 487 |
# Add a title
|
| 488 |
doc.add_heading("AI Customer Ticket Analysis Report", level=1)
|