Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
from PyPDF2 import PdfReader
|
2 |
from markdownify import markdownify
|
|
|
|
|
3 |
|
4 |
# Persistent System Prompt
|
5 |
LOSSDOG_PROMPT = """
|
@@ -26,11 +28,18 @@ def extract_text_from_file(file_path: str, file_name: str) -> str:
|
|
26 |
if file_name.endswith(".pdf"):
|
27 |
try:
|
28 |
pdf_reader = PdfReader(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
return f.read()
|
30 |
except Exception as e:
|
31 |
return f"Error reading text file: {str(e)}"
|
32 |
-
|
33 |
-
|
34 |
|
35 |
def convert_to_markdown(text: str) -> str:
|
36 |
"""Convert extracted file text to Markdown for neat display."""
|
@@ -46,15 +55,6 @@ def interact_with_lossdog(
|
|
46 |
Generates the assistant's response, always including the resume content as context
|
47 |
alongside the conversation history.
|
48 |
"""
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
try:
|
59 |
openai.api_key = api_key
|
60 |
|
@@ -85,8 +85,6 @@ def interact_with_lossdog(
|
|
85 |
validated_history.append({"role": "user", "content": user_message})
|
86 |
validated_history.append({"role": "assistant", "content": assistant_response})
|
87 |
|
88 |
-
|
89 |
-
|
90 |
return validated_history
|
91 |
except Exception as e:
|
92 |
# Append the error as an assistant message (for visibility)
|
@@ -156,7 +154,6 @@ def create_demo():
|
|
156 |
Called when the user sends a new message. We pass the stored resume + history.
|
157 |
"""
|
158 |
updated_history = interact_with_lossdog(user_message, markdown_text, api_key, history)
|
159 |
-
|
160 |
return updated_history, updated_history
|
161 |
|
162 |
# Link File Upload -> handle_upload
|
@@ -187,4 +184,6 @@ def create_demo():
|
|
187 |
|
188 |
return demo
|
189 |
|
190 |
-
if __name__ == "__main__":
|
|
|
|
|
|
1 |
from PyPDF2 import PdfReader
|
2 |
from markdownify import markdownify
|
3 |
+
import gradio as gr
|
4 |
+
import openai
|
5 |
|
6 |
# Persistent System Prompt
|
7 |
LOSSDOG_PROMPT = """
|
|
|
28 |
if file_name.endswith(".pdf"):
|
29 |
try:
|
30 |
pdf_reader = PdfReader(file_path)
|
31 |
+
text = "\n".join(page.extract_text() for page in pdf_reader.pages)
|
32 |
+
return text
|
33 |
+
except Exception as e:
|
34 |
+
return f"Error reading PDF file: {str(e)}"
|
35 |
+
elif file_name.endswith(".txt"):
|
36 |
+
try:
|
37 |
+
with open(file_path, "r") as f:
|
38 |
return f.read()
|
39 |
except Exception as e:
|
40 |
return f"Error reading text file: {str(e)}"
|
41 |
+
else:
|
42 |
+
return "Unsupported file format. Please upload a PDF or TXT file."
|
43 |
|
44 |
def convert_to_markdown(text: str) -> str:
|
45 |
"""Convert extracted file text to Markdown for neat display."""
|
|
|
55 |
Generates the assistant's response, always including the resume content as context
|
56 |
alongside the conversation history.
|
57 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
try:
|
59 |
openai.api_key = api_key
|
60 |
|
|
|
85 |
validated_history.append({"role": "user", "content": user_message})
|
86 |
validated_history.append({"role": "assistant", "content": assistant_response})
|
87 |
|
|
|
|
|
88 |
return validated_history
|
89 |
except Exception as e:
|
90 |
# Append the error as an assistant message (for visibility)
|
|
|
154 |
Called when the user sends a new message. We pass the stored resume + history.
|
155 |
"""
|
156 |
updated_history = interact_with_lossdog(user_message, markdown_text, api_key, history)
|
|
|
157 |
return updated_history, updated_history
|
158 |
|
159 |
# Link File Upload -> handle_upload
|
|
|
184 |
|
185 |
return demo
|
186 |
|
187 |
+
if __name__ == "__main__":
|
188 |
+
demo = create_demo()
|
189 |
+
demo.launch()
|