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