JaphetHernandez commited on
Commit
3250271
·
verified ·
1 Parent(s): efde512

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -44
app.py CHANGED
@@ -14,10 +14,10 @@ login(huggingface_token)
14
  # model_name = "meta-llama/Llama-3.2-3B-Instruct"
15
  model_name = "meta-llama/Llama-3.2-1B-Instruct"
16
  model = AutoModelForCausalLM.from_pretrained(model_name)
17
- tokenizer = AutoTokenizer.from_pretrained(model_name)
18
 
19
  #pipe = pipeline(model=model, tokenizer=tokenizer, max_length = 512) # Check documentation without "feature-extraction"
20
- pipe = pipeline(task = 'text-generation', model=model, tokenizer=tokenizer, max_length = 512) # Check documentation without "feature-extraction"
21
 
22
 
23
  # Use transformers pipeline
@@ -35,47 +35,38 @@ uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
35
  print("Query: ", query)
36
 
37
  if uploaded_file is not None:
38
- # Read CSV file
39
  df = pd.read_csv(uploaded_file)
40
 
41
- job_title = df['job_title']
42
- # Request a query to user
43
- #query = st.text_input("Enter your query:")
44
-
45
- if query:
46
- st.write("Query", query)
47
- st.write("Job Title:" , job_title)
48
- # Create the prompt to calculate cosine similarity score
49
- prompt = f"""
50
- I will provide you a list of candidates' job titles and the search query, I need you to calculate the cosine similarity score between each record and the query.
51
- You should create a new column called 'Score' with those scores and then sort the records from highest to lowest 'Score'.
52
- The query is: {query} and the list of job titles is: {job_title}
53
- """
54
-
55
- # Call the model with the prompt
56
- try:
57
- response = llm_pipeline(prompt, max_length = 512, max_new_tokens = 300, truncation = True, padding = True)
58
- # Show Model answer
59
- st.write("Model Answer:", response)
60
-
61
- except Exception as e:
62
- st.error(f"Error while processing: {str(e)}")
63
-
64
- # Data validation Pydantic
65
- class ConsultModel(BaseModel):
66
- query: str
67
-
68
- @model_validator(mode='before')
69
- def validate_query(cls, values):
70
- query = values.get('query')
71
- if not query:
72
- raise ValueError("Query cannot be empty.")
73
- return values
74
-
75
- # Example
76
- if query:
77
- try:
78
- valid_query = ConsultModel(query=query)
79
- st.success("Valid consult.")
80
- except ValueError as e:
81
- st.error(f"Validation error: {e}")
 
14
  # model_name = "meta-llama/Llama-3.2-3B-Instruct"
15
  model_name = "meta-llama/Llama-3.2-1B-Instruct"
16
  model = AutoModelForCausalLM.from_pretrained(model_name)
17
+ tokenizer = AutoTokenizer.from_pretrained(model_name, device_map = 'auto')
18
 
19
  #pipe = pipeline(model=model, tokenizer=tokenizer, max_length = 512) # Check documentation without "feature-extraction"
20
+ pipe = pipeline(task = 'text-generation', model=model, tokenizer=tokenizer, max_length = 512, device = 'auto') # Check documentation without "feature-extraction"
21
 
22
 
23
  # Use transformers pipeline
 
35
  print("Query: ", query)
36
 
37
  if uploaded_file is not None:
 
38
  df = pd.read_csv(uploaded_file)
39
 
40
+ if 'job_title' not in df.columns:
41
+ st.error("The uploaded CSV must contain a 'job_title' column.")
42
+ else:
43
+ job_titles = df['job_title'].tolist()
44
+
45
+ if query:
46
+ st.write("Query:", query)
47
+
48
+ # Crear el prompt para el LLM
49
+ prompt = f"""
50
+ You are an AI assistant. You will be given a list of job titles and a search query.
51
+ Your task is to calculate the cosine similarity score between the query and each job title.
52
+ You should then return a sorted list of job titles based on the similarity score, from highest to lowest.
53
+ Provide the output in the following format:
54
+
55
+ 1. Job Title: [Job Title], Score: [Cosine Similarity Score]
56
+ 2. Job Title: [Job Title], Score: [Cosine Similarity Score]
57
+ ...
58
+
59
+ Query: {query}
60
+ Job Titles: {job_titles}
61
+ """
62
+
63
+ # Llamar al modelo con el prompt
64
+ try:
65
+ response = pipe(prompt, max_length=1024, num_return_sequences=1)
66
+
67
+ # Mostrar la respuesta del modelo
68
+ st.write("Model Answer:")
69
+ st.write(response[0]['generated_text'])
70
+
71
+ except Exception as e:
72
+ st.error(f"Error while processing: {str(e)}")