barghavani commited on
Commit
8d2e36c
·
verified ·
1 Parent(s): 6b0fe38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -66
app.py CHANGED
@@ -2,12 +2,44 @@ import gradio as gr
2
  import io
3
  import PyPDF2
4
  from langchain.llms import OpenAIChat
5
- from langchain.chains import ConversationChain
6
  from langchain.memory import ConversationBufferMemory
7
- from langchain import LLMChain, PromptTemplate
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- # Limited functionality - Replace with your actual OpenAI API Key
10
- os.environ['OPENAI_API_KEY'] = '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' # Placeholder, needs replacement
11
 
12
  template = """Format the provided resume to this YAML template:
13
  ---
@@ -21,22 +53,22 @@ emails:
21
  dateOfBirth: ''
22
  addresses:
23
  - street: ''
24
-  city: ''
25
-  state: ''
26
-  zip: ''
27
-  country: ''
28
  summary: ''
29
  education:
30
  - school: ''
31
-  degree: ''
32
-  fieldOfStudy: ''
33
-  startDate: ''
34
-  endDate: ''
35
  workExperience:
36
  - company: ''
37
-  position: ''
38
-  startDate: ''
39
-  endDate: ''
40
  skills:
41
  - name: ''
42
  certifications:
@@ -59,55 +91,39 @@ llm_chain = LLMChain(
59
  memory=memory,
60
  )
61
 
62
- testing = False # Set this to True for testing with a fixed PDF
63
-
64
- def format_resume_to_yaml(api_key, pdf_file):
65
- """
66
- This function is limited due to security concerns. It cannot directly accept the
67
- OpenAI API key from the user as input. You'll need to implement server-side
68
- authentication for the API key.
69
- """
70
- if pdf_file is None:
71
- raise ValueError("No PDF file uploaded!")
72
- # Security Risk - Do not accept API key from user input
73
- # if api_key != os.environ.get('OPENAI_API_KEY'):
74
- # raise ValueError("Invalid API Key")
75
-
76
- print(f"API Key: {api_key}") # Log the API Key (for debugging, not for production)
77
-
78
- if testing:
79
- # Use a fixed path for testing (replace with your test PDF path)
80
- with open("path/to/your/test_resume.pdf", "rb") as f:
81
- pdf_file = f
82
- else:
83
- # Use the uploaded PDF from Gradio
84
- pdf_data = pdf_file.read()
85
- pdf_data = io.BytesIO(pdf_data)
86
-
87
- reader = PyPDF2.PdfReader(pdf_data)
88
- resume_text = ""
89
- num_pages = len(reader.pages)
90
-
91
- for page in range(num_pages):
92
- current_page = reader.pages[page]
93
- page_text = current_page.extract_text()
94
- if page_text:
95
- resume_text += page_text
96
- res = llm_chain.predict(human_input=resume_text)
97
- return res
98
-
99
-
100
- # Gradio interface with testing flag
101
- interface = gr.Interface(
102
- fn=format_resume_to_yaml,
103
- inputs=[
104
- gr.File(label="Upload your resume (PDF)"),
105
- # Security Risk - Not recommended to take API key from user input
106
- gr.Textbox(label="Enter your OpenAI API Key (not recommended)"),
107
- ],
108
- outputs="text",
109
- title="Resume to YAML Formatter",
110
- description="Upload your resume (PDF) and get it formatted in YAML. **Please note: Due to security concerns, entering your OpenAI API Key directly is",
111
- )
112
 
113
- interface.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import io
3
  import PyPDF2
4
  from langchain.llms import OpenAIChat
5
+ from langchain.chains import LLMChain
6
  from langchain.memory import ConversationBufferMemory
7
+ from langchain import PromptTemplate
8
+
9
+ def process_pdf(file_info, open_ai_key):
10
+ file_content = file_info["content"]
11
+ extracted_text = extract_text_from_pdf_bytes(file_content)
12
+
13
+ # Set the OpenAI API key
14
+ os.environ['OPENAI_API_KEY'] = open_ai_key
15
+
16
+ # Format the resume text to YAML
17
+ formatted_resume_yaml = format_resume_to_yaml(extracted_text)
18
+
19
+ return formatted_resume_yaml
20
+
21
+ # Assuming you have already defined the template, prompt, memory, and llm_chain as before
22
+
23
+ def format_resume_to_yaml(resume):
24
+ """
25
+ Formats the resume text into a YAML format using the LLMChain.
26
+ """
27
+ # Before calling the LLMChain, ensure the OPENAI_API_KEY environment variable is set
28
+ res = llm_chain.predict(human_input=resume)
29
+ return res.output # Assuming the 'predict' method returns an object with an 'output' attribute containing the result
30
+
31
+ def process_pdf(file_info, open_ai_key):
32
+ file_content = file_info["content"] # Extract file content as bytes
33
+ extracted_text = extract_text_from_pdf_bytes(file_content)
34
+
35
+ # Set the OpenAI API key
36
+ os.environ['OPENAI_API_KEY'] = open_ai_key
37
+
38
+ # Format the resume text to YAML
39
+ formatted_resume_yaml = format_resume_to_yaml(extracted_text)
40
+
41
+ return formatted_resume_yaml
42
 
 
 
43
 
44
  template = """Format the provided resume to this YAML template:
45
  ---
 
53
  dateOfBirth: ''
54
  addresses:
55
  - street: ''
56
+ city: ''
57
+ state: ''
58
+ zip: ''
59
+ country: ''
60
  summary: ''
61
  education:
62
  - school: ''
63
+ degree: ''
64
+ fieldOfStudy: ''
65
+ startDate: ''
66
+ endDate: ''
67
  workExperience:
68
  - company: ''
69
+ position: ''
70
+ startDate: ''
71
+ endDate: ''
72
  skills:
73
  - name: ''
74
  certifications:
 
91
  memory=memory,
92
  )
93
 
94
+ def format_resume_to_yaml(resume):
95
+ # Before calling the LLMChain, ensure the OPENAI_API_KEY environment variable is set
96
+ res = llm_chain.predict(human_input=resume)
97
+ return res
98
+
99
+ def process_pdf(file, open_ai_key):
100
+ # Save the uploaded PDF temporarily
101
+ temp_pdf_path = "/tmp/uploaded_resume.pdf"
102
+ with open(temp_pdf_path, 'wb') as f:
103
+ f.write(file.read())
104
+
105
+ # Extract text from the PDF
106
+ extracted_text = extract_text_from_pdf(temp_pdf_path)
107
+
108
+ # Set the OpenAI API key
109
+ os.environ['OPENAI_API_KEY'] = open_ai_key
110
+
111
+ # Format the resume text to YAML
112
+ formatted_resume_yaml = format_resume_to_yaml(extracted_text)
113
+
114
+ return formatted_resume_yaml
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
+ with gr.Blocks() as demo:
117
+ gr.Markdown("### Upload a Resume in PDF Format")
118
+ with gr.Row():
119
+ pdf_file = gr.File(label="Select a PDF file", type="file")
120
+ openai_key_input = gr.Textbox(label="OpenAI API Key", type="password")
121
+ format_button = gr.Button("Format Resume")
122
+ output_textbox = gr.Textbox(label="Formatted Resume in YAML", lines=20)
123
+
124
+ format_button.click(
125
+ fn=process_pdf,
126
+ inputs=[pdf_file, openai_key_input],
127
+ outputs=[output_textbox]
128
+ )
129
+ demo.launch(debug=True)