jzou19950715 commited on
Commit
f090183
·
verified ·
1 Parent(s): d9f57d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -50
app.py CHANGED
@@ -42,11 +42,6 @@ LOSSDOG_PROMPT = """
42
  def extract_text_from_file(file_path: str, file_name: str) -> str:
43
  """
44
  Extract text from a file. Handles PDF and plain text files.
45
- Args:
46
- file_path (str): The path to the file.
47
- file_name (str): The name of the file.
48
- Returns:
49
- str: Extracted text content from the file.
50
  """
51
  if file_name.endswith(".pdf"):
52
  try:
@@ -67,24 +62,12 @@ def extract_text_from_file(file_path: str, file_name: str) -> str:
67
  def convert_to_markdown(text: str) -> str:
68
  """
69
  Convert plain text to Markdown format for better readability.
70
- Args:
71
- text (str): The plain text to convert.
72
- Returns:
73
- str: The Markdown-formatted text.
74
  """
75
  return markdownify(text, heading_style="ATX")
76
 
77
- def analyze_resume_with_lossdog(
78
- markdown_text: str, api_key: str, history: list
79
- ) -> list:
80
  """
81
  Analyze the resume using OpenAI GPT and generate conversational responses.
82
- Args:
83
- markdown_text (str): Markdown content of the resume.
84
- api_key (str): OpenAI API key.
85
- history (list): Chat history between the user and the assistant.
86
- Returns:
87
- list: Updated chat history with AI responses.
88
  """
89
  try:
90
  openai.api_key = api_key
@@ -94,7 +77,7 @@ def analyze_resume_with_lossdog(
94
  ] + history
95
 
96
  response = openai.ChatCompletion.create(
97
- model="gpt-4",
98
  messages=messages,
99
  max_tokens=4096, # Maximum token limit for GPT-4
100
  )
@@ -105,23 +88,6 @@ def analyze_resume_with_lossdog(
105
  history.append({"role": "assistant", "content": f"Error: {str(e)}"})
106
  return history
107
 
108
- def generate_json_output(history: list) -> tuple:
109
- """
110
- Generate a JSON summary of the chat history and save it to a file.
111
- Args:
112
- history (list): Chat history between the user and the assistant.
113
- Returns:
114
- tuple: File name and content of the generated JSON file.
115
- """
116
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
117
- filename = f"resume_analysis_{timestamp}.json"
118
- try:
119
- output_data = {"conversation_history": history}
120
- with open(filename, "w", encoding="utf-8") as f:
121
- json.dump(output_data, f, indent=2)
122
- return filename, json.dumps(output_data, f, indent=2)
123
- except Exception as e:
124
- return None, f"Error generating JSON: {str(e)}"
125
  def create_demo():
126
  """
127
  Create the Gradio Blocks interface for the Loss Dog resume analyzer.
@@ -131,32 +97,41 @@ def create_demo():
131
  # 🐕 LOSS Dog: AI-Powered Resume Analyzer and Builder
132
 
133
  Upload your resume (PDF or TXT), and Loss Dog will:
134
- - Display it in a box in the top-right corner.
135
- - Proactively analyze it and start a conversation.
136
- - Help you build a polished, professional resume with actionable feedback.
137
  """)
138
 
139
- # Main layout: Left for chat, top-right for resume preview
140
- with gr.Row():
141
- chatbot = gr.Chatbot(label="Chat with LOSS Dog", type="messages")
 
 
 
 
142
 
143
- # Small top-right container for resume preview
 
 
 
144
  with gr.Column(scale=1):
145
- markdown_preview = gr.Markdown(label="Resume Preview") # Fixed: Removed interactive=False
146
 
 
147
  with gr.Row():
148
  user_input = gr.Textbox(
149
  placeholder="Ask LOSS Dog about your resume...",
150
- label="Your Message"
 
151
  )
152
  send_button = gr.Button("Send 🐾")
153
 
154
- # File upload and JSON generation in a separate row
155
  with gr.Row():
156
  upload = gr.File(label="Upload Your Resume (PDF or TXT)")
157
  json_view = gr.JSON(label="Final JSON Summary")
158
 
159
- # App state
160
  history_state = gr.State([])
161
  markdown_state = gr.State("")
162
 
@@ -164,18 +139,18 @@ def create_demo():
164
  if not file:
165
  return "No file uploaded.", [], None
166
 
167
- # Extract resume content from the uploaded file
168
  file_path = file.name
169
  file_content = extract_text_from_file(file_path, file.name)
170
  if "Error" in file_content:
171
  return file_content, [], None
172
 
173
- # Convert extracted content to Markdown for display
174
  markdown_text = convert_to_markdown(file_content)
175
  history = [
176
  {
177
  "role": "assistant",
178
- "content": "I've reviewed your resume. Let's dive into ways to improve it! Here are some initial thoughts:",
179
  }
180
  ]
181
  history = analyze_resume_with_lossdog(markdown_text, api_key, history)
 
42
  def extract_text_from_file(file_path: str, file_name: str) -> str:
43
  """
44
  Extract text from a file. Handles PDF and plain text files.
 
 
 
 
 
45
  """
46
  if file_name.endswith(".pdf"):
47
  try:
 
62
  def convert_to_markdown(text: str) -> str:
63
  """
64
  Convert plain text to Markdown format for better readability.
 
 
 
 
65
  """
66
  return markdownify(text, heading_style="ATX")
67
 
68
+ def analyze_resume_with_lossdog(markdown_text: str, api_key: str, history: list) -> list:
 
 
69
  """
70
  Analyze the resume using OpenAI GPT and generate conversational responses.
 
 
 
 
 
 
71
  """
72
  try:
73
  openai.api_key = api_key
 
77
  ] + history
78
 
79
  response = openai.ChatCompletion.create(
80
+ model="gpt-4o-mini",
81
  messages=messages,
82
  max_tokens=4096, # Maximum token limit for GPT-4
83
  )
 
88
  history.append({"role": "assistant", "content": f"Error: {str(e)}"})
89
  return history
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  def create_demo():
92
  """
93
  Create the Gradio Blocks interface for the Loss Dog resume analyzer.
 
97
  # 🐕 LOSS Dog: AI-Powered Resume Analyzer and Builder
98
 
99
  Upload your resume (PDF or TXT), and Loss Dog will:
100
+ - Display your resume in the top-right box.
101
+ - Proactively analyze it and provide actionable feedback.
102
+ - Help you build a professional and tailored resume.
103
  """)
104
 
105
+ # API Key Input
106
+ api_key = gr.Textbox(
107
+ label="OpenAI API Key",
108
+ placeholder="Enter your OpenAI API key...",
109
+ type="password",
110
+ show_label=True
111
+ )
112
 
113
+ # Main Layout: Chatbot on the left, resume preview on the right
114
+ with gr.Row():
115
+ with gr.Column(scale=3):
116
+ chatbot = gr.Chatbot(label="Chat with LOSS Dog", type="messages")
117
  with gr.Column(scale=1):
118
+ markdown_preview = gr.Markdown(label="Resume Preview")
119
 
120
+ # User input and Send button
121
  with gr.Row():
122
  user_input = gr.Textbox(
123
  placeholder="Ask LOSS Dog about your resume...",
124
+ label="Your Message",
125
+ lines=1
126
  )
127
  send_button = gr.Button("Send 🐾")
128
 
129
+ # File upload and JSON summary
130
  with gr.Row():
131
  upload = gr.File(label="Upload Your Resume (PDF or TXT)")
132
  json_view = gr.JSON(label="Final JSON Summary")
133
 
134
+ # App State
135
  history_state = gr.State([])
136
  markdown_state = gr.State("")
137
 
 
139
  if not file:
140
  return "No file uploaded.", [], None
141
 
142
+ # Extract resume content
143
  file_path = file.name
144
  file_content = extract_text_from_file(file_path, file.name)
145
  if "Error" in file_content:
146
  return file_content, [], None
147
 
148
+ # Convert to Markdown
149
  markdown_text = convert_to_markdown(file_content)
150
  history = [
151
  {
152
  "role": "assistant",
153
+ "content": "I've reviewed your resume. Let's discuss improvements! Here are my thoughts:",
154
  }
155
  ]
156
  history = analyze_resume_with_lossdog(markdown_text, api_key, history)