Karthikeyan
Update app.py
9d96e31
import gradio as gr
import openai
import os
import PyPDF2
from docx import Document
openai.api_key = os.getenv("OPENAI_API_KEY") # replace with your OpenAI key
def extract_text_from_file(file_path):
file_extension = os.path.splitext(file_path)[1]
if file_extension == '.pdf':
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfFileReader(file)
text = ""
for page_number in range(reader.getNumPages()):
page = reader.getPage(page_number)
text += page.extractText()
return text
elif file_extension == '.txt':
with open(file_path, 'r') as file:
text = file.read()
return text
elif file_extension == '.docx':
doc = Document(file_path)
text = []
for paragraph in doc.paragraphs:
text.append(paragraph.text)
text='\n'.join(text)
return text
else:
return "Unsupported file type"
def gpt3_prompt(prompt):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
temperature=0,
max_tokens=200
)
return response.choices[0].text.strip()
def respond(message, chat_history, file_path):
try:
if file_path is not None:
text = extract_text_from_file(file_path.name)
text = text + " " + message
# else:
# text = "Please Update Resume!!"
prompt=f"""Your are HR chatbot. Answer for user Queries with user friendly and proper output following Instructions if .
Instructions:
1. Aanalyse the text to Generate Job Description For Given Job title and Experience. if User ask this ask to user Job Title and Experience and return Response.
2. Aanalyse the text to Prepare questions from job Description. If User ask this ask to user Upload file and generate and return responce from that file
3. Aanalyse the text to Find Education Gaps in given resume. Find Skills in resume. If User ask this ask to user Upload file and generate and return responce from that file
4. Aanalyse the text to to generate online courses with website links. If User ask this ask to user Upload file and generate and return responce from that file Generate atmost five courses.
5. If the text not available continue normal HR Chatbot
text : {text}
"""
bot_message = gpt3_prompt(prompt)
chat_history.append((message, bot_message))
return "", chat_history
except:
bot_message = "Please Upload Resume!!"
chat_history.append((message, bot_message))
return "", chat_history
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.ClearButton([msg, chatbot])
uploader = gr.inputs.File()
msg.submit(respond, [msg, chatbot, uploader], [msg, chatbot])
demo.launch()