Update login_module/chat.py
Browse files- login_module/chat.py +37 -6
login_module/chat.py
CHANGED
@@ -13,22 +13,53 @@ load_dotenv()
|
|
13 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
text_model = genai.GenerativeModel("gemini-2.5-flash")
|
15 |
|
16 |
-
# --- Helper Functions (Logic from Streamlit Chat) ---
|
17 |
|
18 |
-
|
|
|
19 |
"""Processes the uploaded PDF file."""
|
20 |
-
|
|
|
|
|
|
|
21 |
return ""
|
|
|
22 |
try:
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
reader = PyPDF2.PdfReader(f)
|
25 |
text = ""
|
26 |
for page in reader.pages:
|
27 |
text += page.extract_text()
|
28 |
return text
|
29 |
-
except
|
30 |
-
|
|
|
31 |
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def getallinfo_chat(data):
|
34 |
"""Formats resume data."""
|
|
|
13 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
text_model = genai.GenerativeModel("gemini-2.5-flash")
|
15 |
|
|
|
16 |
|
17 |
+
|
18 |
+
def file_processing_chat(pdf_file_obj): # Take the Gradio file object
|
19 |
"""Processes the uploaded PDF file."""
|
20 |
+
# --- CORRECTION: Handle the Gradio file object correctly ---
|
21 |
+
# Check if the input is None or falsy
|
22 |
+
if not pdf_file_obj:
|
23 |
+
print("No file object provided to file_processing_chat.")
|
24 |
return ""
|
25 |
+
|
26 |
try:
|
27 |
+
# Determine the correct file path from the Gradio object
|
28 |
+
# Gradio File component usually provides an object with a 'name' attribute
|
29 |
+
# containing the path to the temporary file.
|
30 |
+
if hasattr(pdf_file_obj, 'name'):
|
31 |
+
# This is the standard way for Gradio File uploads
|
32 |
+
file_path = pdf_file_obj.name
|
33 |
+
else:
|
34 |
+
# Fallback: If it's already a string path (less common in recent Gradio)
|
35 |
+
# or if the structure is different, try using it directly.
|
36 |
+
# Converting to string is a safe fallback.
|
37 |
+
file_path = str(pdf_file_obj)
|
38 |
+
print(f"File object does not have 'name' attribute. Using str(): {file_path}")
|
39 |
+
|
40 |
+
print(f"Attempting to process file at path: {file_path}")
|
41 |
+
|
42 |
+
# --- Use the file path with PyPDF2 ---
|
43 |
+
# Open the file using the resolved path string
|
44 |
+
with open(file_path, "rb") as f: # Open in binary read mode
|
45 |
reader = PyPDF2.PdfReader(f)
|
46 |
text = ""
|
47 |
for page in reader.pages:
|
48 |
text += page.extract_text()
|
49 |
return text
|
50 |
+
except FileNotFoundError:
|
51 |
+
error_msg = f"File not found at path: {file_path}"
|
52 |
+
print(error_msg)
|
53 |
return ""
|
54 |
+
except PyPDF2.errors.PdfReadError as e:
|
55 |
+
error_msg = f"Error reading PDF file {file_path}: {e}"
|
56 |
+
print(error_msg)
|
57 |
+
return ""
|
58 |
+
except Exception as e: # Catch other potential errors during file handling
|
59 |
+
error_msg = f"Unexpected error processing PDF from object {pdf_file_obj}: {e}"
|
60 |
+
print(error_msg)
|
61 |
+
return ""
|
62 |
+
|
63 |
|
64 |
def getallinfo_chat(data):
|
65 |
"""Formats resume data."""
|