Ahmed007 commited on
Commit
a86fdea
·
verified ·
1 Parent(s): badc390

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +90 -0
main.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import os
3
+ import nltk
4
+ from langchain.llms import LlamaCpp
5
+ from langchain.callbacks.manager import CallbackManager
6
+ from langchain.prompts import PromptTemplate
7
+ from langchain.schema.output_parser import StrOutputParser
8
+
9
+ nltk.download('punkt')
10
+
11
+ app = Flask(__name__)
12
+
13
+ # Download model
14
+ if not os.path.exists('phi-2.Q4_K_M.gguf'):
15
+ os.system('wget https://huggingface.co/TheBloke/phi-2-GGUF/resolve/main/phi-2.Q4_K_M.gguf')
16
+
17
+ # Disable GPU usage
18
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
19
+
20
+ # Callback manager setup
21
+ callback_manager = CallbackManager([])
22
+
23
+ # Creating LlamaCpp instance
24
+ llm = LlamaCpp(
25
+ model_path="phi-2.Q4_K_M.gguf",
26
+ temperature=0.1,
27
+ n_gpu_layers=0,
28
+ n_batch=1024,
29
+ callback_manager=callback_manager,
30
+ verbose=True,
31
+ n_ctx=2048
32
+ )
33
+
34
+ # Define templates
35
+ templates = {
36
+ "work_experience": """Instruction:
37
+ Extract and summarize the work experience mentioned in the CV provided below. Focus solely on the details related to work history, including job titles, companies, and duration.
38
+ Text: {text}
39
+ Question: {question}
40
+ Output:""",
41
+
42
+ "certification": """Instruction:
43
+ Extract and summarize the certification history mentioned in the CV provided below. Include details such as degrees earned, institutions attended, and graduation years.
44
+ Text: {text}
45
+ Question: {question}
46
+ Output:""",
47
+
48
+ "contact_info": """Instruction:
49
+ Extract and provide the contact information mentioned in the CV provided below. Include details such as phone number, email address, and any other relevant contact links.
50
+ Text: {text}
51
+ Question: {question}
52
+ Output:""",
53
+
54
+ "skills": """Instruction:
55
+ Focus solely on extracting the skills mentioned in the text below, excluding any other details or context. Your answer should consist of concise skills.
56
+ Text: {text}
57
+ Question: {question}
58
+ Output:"""
59
+ }
60
+
61
+ @app.route('/', methods=['POST'])
62
+ def generate_text():
63
+ data = request.get_json()
64
+ question = data.get('question')
65
+ text = data.get('text')
66
+
67
+ if not question or not text:
68
+ return jsonify({"error": "Both 'question' and 'text' fields are required."}), 400
69
+
70
+ if question == "Please summarize the work experience mentioned in the CV.":
71
+ template_key = "work_experience"
72
+ elif question == "Please summarize the certification history mentioned in the CV without repeating the output only once.":
73
+ template_key = "certification"
74
+ elif question == "Please extract the contact information mentioned in the CV once.":
75
+ template_key = "contact_info"
76
+ elif question == "What are the 6 skills? Please provide a concise short answer of the only(skills) mentioned in the text without repeating the answer.":
77
+ template_key = "skills"
78
+ else:
79
+ return jsonify({"error": "Invalid question provided."}), 400
80
+
81
+ prompt = PromptTemplate(template=templates[template_key], input_variables=["question", "text"])
82
+ chain = prompt | llm | StrOutputParser()
83
+ response = chain.invoke({"question": question, "text": text})
84
+
85
+ return jsonify({"generated_text": response})
86
+
87
+
88
+ if __name__ == '__main__':
89
+ port = int(os.environ.get("PORT", 8000))
90
+ app.run( port= 8000)