Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,20 +27,10 @@ LOSSDOG_PROMPT = """
|
|
| 27 |
"""
|
| 28 |
|
| 29 |
def extract_text_from_file(file_path: str, file_name: str) -> str:
|
| 30 |
-
"""
|
| 31 |
-
Extract text from a file. Handles PDF and plain text files.
|
| 32 |
-
Args:
|
| 33 |
-
file_path (str): The path to the file.
|
| 34 |
-
file_name (str): The name of the file.
|
| 35 |
-
Returns:
|
| 36 |
-
str: Extracted text content from the file.
|
| 37 |
-
"""
|
| 38 |
if file_name.endswith(".pdf"):
|
| 39 |
try:
|
| 40 |
pdf_reader = PdfReader(file_path)
|
| 41 |
-
text = ""
|
| 42 |
-
for page in pdf_reader.pages:
|
| 43 |
-
text += page.extract_text()
|
| 44 |
return text
|
| 45 |
except Exception as e:
|
| 46 |
return f"Error extracting text from PDF: {str(e)}"
|
|
@@ -54,31 +44,15 @@ def extract_text_from_file(file_path: str, file_name: str) -> str:
|
|
| 54 |
return "Unsupported file format. Please upload a PDF or TXT file."
|
| 55 |
|
| 56 |
def convert_to_markdown(text: str) -> str:
|
| 57 |
-
"""
|
| 58 |
-
Convert plain text to Markdown format for better readability.
|
| 59 |
-
Args:
|
| 60 |
-
text (str): The plain text to convert.
|
| 61 |
-
Returns:
|
| 62 |
-
str: The Markdown-formatted text.
|
| 63 |
-
"""
|
| 64 |
return markdownify(text, heading_style="ATX")
|
| 65 |
|
| 66 |
def analyze_resume_with_lossdog(
|
| 67 |
markdown_text: str, api_key: str, history: list
|
| 68 |
) -> list:
|
| 69 |
-
"""
|
| 70 |
-
Analyze the resume using GPT-4o-mini and generate conversational responses.
|
| 71 |
-
Args:
|
| 72 |
-
markdown_text (str): Markdown content of the resume.
|
| 73 |
-
api_key (str): OpenAI API key.
|
| 74 |
-
history (list): Chat history between the user and the assistant.
|
| 75 |
-
Returns:
|
| 76 |
-
list: Updated chat history with AI responses.
|
| 77 |
-
"""
|
| 78 |
try:
|
| 79 |
openai.api_key = api_key
|
| 80 |
messages = [
|
| 81 |
-
{"role": "system", "content":
|
| 82 |
{"role": "system", "content": f"Resume Content:\n{markdown_text}"},
|
| 83 |
] + history
|
| 84 |
|
|
@@ -95,13 +69,6 @@ def analyze_resume_with_lossdog(
|
|
| 95 |
return history
|
| 96 |
|
| 97 |
def generate_json_output(history: list) -> tuple:
|
| 98 |
-
"""
|
| 99 |
-
Generate a JSON summary of the chat history and save it to a file.
|
| 100 |
-
Args:
|
| 101 |
-
history (list): Chat history between the user and the assistant.
|
| 102 |
-
Returns:
|
| 103 |
-
tuple: File name and content of the generated JSON file.
|
| 104 |
-
"""
|
| 105 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 106 |
filename = f"resume_analysis_{timestamp}.json"
|
| 107 |
try:
|
|
@@ -113,9 +80,6 @@ def generate_json_output(history: list) -> tuple:
|
|
| 113 |
return None, f"Error generating JSON: {str(e)}"
|
| 114 |
|
| 115 |
def create_demo():
|
| 116 |
-
"""
|
| 117 |
-
Create the Gradio Blocks interface for the Loss Dog resume analyzer.
|
| 118 |
-
"""
|
| 119 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 120 |
gr.Markdown("""
|
| 121 |
# 🐕 LOSS Dog: AI-Powered Resume Analyzer
|
|
@@ -137,7 +101,7 @@ def create_demo():
|
|
| 137 |
upload = gr.File(label="Upload Your Resume (PDF or TXT)")
|
| 138 |
|
| 139 |
markdown_preview = gr.Markdown(label="Resume Content (Markdown)")
|
| 140 |
-
chatbot = gr.Chatbot(label="Chat with LOSS Dog")
|
| 141 |
|
| 142 |
with gr.Row():
|
| 143 |
user_input = gr.Textbox(
|
|
@@ -148,17 +112,13 @@ def create_demo():
|
|
| 148 |
|
| 149 |
with gr.Row():
|
| 150 |
json_view = gr.JSON(label="Final JSON Summary")
|
| 151 |
-
|
| 152 |
|
| 153 |
-
# Internal states for the Gradio app
|
| 154 |
history_state = gr.State([])
|
| 155 |
markdown_state = gr.State("")
|
| 156 |
file_path_state = gr.State("")
|
| 157 |
|
| 158 |
def handle_upload(file, api_key):
|
| 159 |
-
"""
|
| 160 |
-
Handle resume file upload. Convert to Markdown and update chat history.
|
| 161 |
-
"""
|
| 162 |
if not file:
|
| 163 |
return None, "No file uploaded.", [], None
|
| 164 |
|
|
@@ -177,48 +137,38 @@ def create_demo():
|
|
| 177 |
return file, markdown_text, history, markdown_text
|
| 178 |
|
| 179 |
def handle_message(message, api_key, history, markdown_text):
|
| 180 |
-
"""
|
| 181 |
-
Process user messages and generate Loss Dog's response.
|
| 182 |
-
"""
|
| 183 |
if not message.strip():
|
| 184 |
return history
|
| 185 |
|
| 186 |
return analyze_resume_with_lossdog(markdown_text, api_key, history)
|
| 187 |
|
| 188 |
def handle_generate_json(history):
|
| 189 |
-
"""
|
| 190 |
-
Generate JSON summary from the conversation history.
|
| 191 |
-
"""
|
| 192 |
filename, json_content = generate_json_output(history)
|
| 193 |
if not filename:
|
| 194 |
-
return
|
| 195 |
-
|
|
|
|
| 196 |
|
| 197 |
-
# Event: When a file is uploaded
|
| 198 |
upload.change(
|
| 199 |
handle_upload,
|
| 200 |
inputs=[upload, api_key],
|
| 201 |
outputs=[upload, markdown_preview, chatbot, markdown_state]
|
| 202 |
)
|
| 203 |
|
| 204 |
-
# Event: When a message is sent
|
| 205 |
send_button.click(
|
| 206 |
handle_message,
|
| 207 |
inputs=[user_input, api_key, history_state, markdown_state],
|
| 208 |
outputs=[chatbot]
|
| 209 |
)
|
| 210 |
|
| 211 |
-
|
| 212 |
-
download_button.click(
|
| 213 |
handle_generate_json,
|
| 214 |
inputs=[history_state],
|
| 215 |
-
outputs=[json_view,
|
| 216 |
)
|
| 217 |
|
| 218 |
return demo
|
| 219 |
|
| 220 |
-
|
| 221 |
-
# Main execution
|
| 222 |
if __name__ == "__main__":
|
| 223 |
demo = create_demo()
|
| 224 |
demo.launch()
|
|
|
|
| 27 |
"""
|
| 28 |
|
| 29 |
def extract_text_from_file(file_path: str, file_name: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if file_name.endswith(".pdf"):
|
| 31 |
try:
|
| 32 |
pdf_reader = PdfReader(file_path)
|
| 33 |
+
text = "".join([page.extract_text() for page in pdf_reader.pages])
|
|
|
|
|
|
|
| 34 |
return text
|
| 35 |
except Exception as e:
|
| 36 |
return f"Error extracting text from PDF: {str(e)}"
|
|
|
|
| 44 |
return "Unsupported file format. Please upload a PDF or TXT file."
|
| 45 |
|
| 46 |
def convert_to_markdown(text: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
return markdownify(text, heading_style="ATX")
|
| 48 |
|
| 49 |
def analyze_resume_with_lossdog(
|
| 50 |
markdown_text: str, api_key: str, history: list
|
| 51 |
) -> list:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
try:
|
| 53 |
openai.api_key = api_key
|
| 54 |
messages = [
|
| 55 |
+
{"role": "system", "content": LOSS DOG_PROMPT},
|
| 56 |
{"role": "system", "content": f"Resume Content:\n{markdown_text}"},
|
| 57 |
] + history
|
| 58 |
|
|
|
|
| 69 |
return history
|
| 70 |
|
| 71 |
def generate_json_output(history: list) -> tuple:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 73 |
filename = f"resume_analysis_{timestamp}.json"
|
| 74 |
try:
|
|
|
|
| 80 |
return None, f"Error generating JSON: {str(e)}"
|
| 81 |
|
| 82 |
def create_demo():
|
|
|
|
|
|
|
|
|
|
| 83 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 84 |
gr.Markdown("""
|
| 85 |
# 🐕 LOSS Dog: AI-Powered Resume Analyzer
|
|
|
|
| 101 |
upload = gr.File(label="Upload Your Resume (PDF or TXT)")
|
| 102 |
|
| 103 |
markdown_preview = gr.Markdown(label="Resume Content (Markdown)")
|
| 104 |
+
chatbot = gr.Chatbot(label="Chat with LOSS Dog", type="messages")
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
user_input = gr.Textbox(
|
|
|
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
json_view = gr.JSON(label="Final JSON Summary")
|
| 115 |
+
download_link = gr.Textbox(label="Download Link (Copy & Paste)")
|
| 116 |
|
|
|
|
| 117 |
history_state = gr.State([])
|
| 118 |
markdown_state = gr.State("")
|
| 119 |
file_path_state = gr.State("")
|
| 120 |
|
| 121 |
def handle_upload(file, api_key):
|
|
|
|
|
|
|
|
|
|
| 122 |
if not file:
|
| 123 |
return None, "No file uploaded.", [], None
|
| 124 |
|
|
|
|
| 137 |
return file, markdown_text, history, markdown_text
|
| 138 |
|
| 139 |
def handle_message(message, api_key, history, markdown_text):
|
|
|
|
|
|
|
|
|
|
| 140 |
if not message.strip():
|
| 141 |
return history
|
| 142 |
|
| 143 |
return analyze_resume_with_lossdog(markdown_text, api_key, history)
|
| 144 |
|
| 145 |
def handle_generate_json(history):
|
|
|
|
|
|
|
|
|
|
| 146 |
filename, json_content = generate_json_output(history)
|
| 147 |
if not filename:
|
| 148 |
+
return json_content, ""
|
| 149 |
+
download_url = f"/download/{filename}"
|
| 150 |
+
return json_content, download_url
|
| 151 |
|
|
|
|
| 152 |
upload.change(
|
| 153 |
handle_upload,
|
| 154 |
inputs=[upload, api_key],
|
| 155 |
outputs=[upload, markdown_preview, chatbot, markdown_state]
|
| 156 |
)
|
| 157 |
|
|
|
|
| 158 |
send_button.click(
|
| 159 |
handle_message,
|
| 160 |
inputs=[user_input, api_key, history_state, markdown_state],
|
| 161 |
outputs=[chatbot]
|
| 162 |
)
|
| 163 |
|
| 164 |
+
send_button.click(
|
|
|
|
| 165 |
handle_generate_json,
|
| 166 |
inputs=[history_state],
|
| 167 |
+
outputs=[json_view, download_link]
|
| 168 |
)
|
| 169 |
|
| 170 |
return demo
|
| 171 |
|
|
|
|
|
|
|
| 172 |
if __name__ == "__main__":
|
| 173 |
demo = create_demo()
|
| 174 |
demo.launch()
|