Spaces:
Sleeping
Sleeping
Karthikeyan
commited on
Commit
·
63d1ff6
1
Parent(s):
1be46c0
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
import PyPDF2
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import docx
|
| 6 |
+
|
| 7 |
+
class CourseGenarator:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
+
|
| 11 |
+
def extract_text_from_file(self,file_path):
|
| 12 |
+
# Get the file extension
|
| 13 |
+
file_extension = os.path.splitext(file_path)[1]
|
| 14 |
+
|
| 15 |
+
if file_extension == '.pdf':
|
| 16 |
+
with open(file_path, 'rb') as file:
|
| 17 |
+
# Create a PDF file reader object
|
| 18 |
+
reader = PyPDF2.PdfFileReader(file)
|
| 19 |
+
|
| 20 |
+
# Create an empty string to hold the extracted text
|
| 21 |
+
extracted_text = ""
|
| 22 |
+
|
| 23 |
+
# Loop through each page in the PDF and extract the text
|
| 24 |
+
for page_number in range(reader.getNumPages()):
|
| 25 |
+
page = reader.getPage(page_number)
|
| 26 |
+
extracted_text += page.extractText()
|
| 27 |
+
return extracted_text
|
| 28 |
+
|
| 29 |
+
elif file_extension == '.txt':
|
| 30 |
+
with open(file_path, 'r') as file:
|
| 31 |
+
# Just read the entire contents of the text file
|
| 32 |
+
return file.read()
|
| 33 |
+
|
| 34 |
+
elif file_extension == '.docx':
|
| 35 |
+
doc = docx.Document(file_path)
|
| 36 |
+
text = []
|
| 37 |
+
for paragraph in doc.paragraphs:
|
| 38 |
+
text.append(paragraph.text)
|
| 39 |
+
return '\n'.join(text)
|
| 40 |
+
|
| 41 |
+
else:
|
| 42 |
+
return "Unsupported file type"
|
| 43 |
+
|
| 44 |
+
def response(self,resume_path):
|
| 45 |
+
resume_path = resume_path.name
|
| 46 |
+
resume = self.extract_text_from_file(resume_path)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# Define the prompt or input for the model
|
| 50 |
+
prompt = f"""Analyze the resume to generate online courses with website links to improve skills following resume delimitted by triple backticks. Generate atmost five courses.
|
| 51 |
+
result format should be:
|
| 52 |
+
course:[course].
|
| 53 |
+
website link:[website link]
|
| 54 |
+
```{resume}```
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
# Generate a response from the GPT-3 model
|
| 58 |
+
response = openai.Completion.create(
|
| 59 |
+
engine='text-davinci-003',
|
| 60 |
+
prompt=prompt,
|
| 61 |
+
max_tokens=200,
|
| 62 |
+
temperature=0,
|
| 63 |
+
n=1,
|
| 64 |
+
stop=None,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Extract the generated text from the API response
|
| 68 |
+
generated_text = response.choices[0].text.strip()
|
| 69 |
+
|
| 70 |
+
return generated_text
|
| 71 |
+
|
| 72 |
+
def gradio_interface(self):
|
| 73 |
+
with gr.Blocks(css="style.css",theme=gr.themes.Soft()) as app:
|
| 74 |
+
gr.HTML("""<img class="leftimage" align="left" src="https://templates.images.credential.net/1612472097627370951721412474196.png" alt="Image" width="210" height="210">
|
| 75 |
+
<img class="rightimage" align="right" src="https://companieslogo.com/img/orig/RAND.AS_BIG-0f1935a4.png?t=1651813778" alt="Image" width="210" height="210">""")
|
| 76 |
+
|
| 77 |
+
with gr.Row(elem_id="col-container"):
|
| 78 |
+
with gr.Column():
|
| 79 |
+
gr.HTML("<br>")
|
| 80 |
+
gr.HTML(
|
| 81 |
+
"""<h1 style="text-align:center; color:"white">Courses </h1> """
|
| 82 |
+
)
|
| 83 |
+
gr.HTML("<br>")
|
| 84 |
+
with gr.Column():
|
| 85 |
+
resume = gr.File(label="Resume")
|
| 86 |
+
|
| 87 |
+
with gr.Column():
|
| 88 |
+
analyse = gr.Button("Generate")
|
| 89 |
+
|
| 90 |
+
with gr.Column():
|
| 91 |
+
result = gr.Textbox(label="Courses",lines=8)
|
| 92 |
+
|
| 93 |
+
analyse.click(self.response, [resume], result)
|
| 94 |
+
print(result)
|
| 95 |
+
|
| 96 |
+
app.launch()
|
| 97 |
+
|
| 98 |
+
ques = CourseGenarator()
|
| 99 |
+
ques.gradio_interface()
|