File size: 3,071 Bytes
edfb960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c41c673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d96e31
 
 
c41c673
edfb960
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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()