Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,70 +6,70 @@ from langchain.llms import HuggingFacePipeline
|
|
6 |
from huggingface_hub import login
|
7 |
from pydantic import BaseModel, model_validator
|
8 |
|
9 |
-
# Token
|
10 |
huggingface_token = st.secrets["HUGGINGFACEHUB_API_TOKEN"]
|
11 |
login(huggingface_token)
|
12 |
|
13 |
-
#
|
14 |
model_name = "meta-llama/Llama-3.2-3B-Instruct"
|
15 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
16 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
17 |
|
18 |
pipe = pipeline("feature-extraction", model=model, tokenizer=tokenizer)
|
19 |
|
20 |
-
#
|
21 |
llm_pipeline = HuggingFacePipeline(pipeline=pipe)
|
22 |
|
23 |
-
#
|
24 |
-
st.title("Cosine Similarity
|
25 |
|
26 |
|
27 |
-
#
|
28 |
query = ""
|
29 |
|
30 |
-
#
|
31 |
-
uploaded_file = st.file_uploader("
|
32 |
|
33 |
|
34 |
if uploaded_file is not None:
|
35 |
-
#
|
36 |
df = pd.read_csv(uploaded_file)
|
37 |
|
38 |
-
#
|
39 |
-
query = st.text_input("
|
40 |
|
41 |
if query:
|
42 |
-
#
|
43 |
prompt = f"""
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
"""
|
48 |
|
49 |
-
#
|
50 |
try:
|
51 |
response = llm_pipeline(prompt)
|
52 |
-
#
|
53 |
-
st.write("
|
54 |
|
55 |
except Exception as e:
|
56 |
-
st.error(f"
|
57 |
|
58 |
-
#
|
59 |
-
class
|
60 |
query: str
|
61 |
|
62 |
@model_validator(mode='before')
|
63 |
-
def
|
64 |
query = values.get('query')
|
65 |
if not query:
|
66 |
-
raise ValueError("
|
67 |
return values
|
68 |
|
69 |
-
#
|
70 |
if query:
|
71 |
try:
|
72 |
-
valid_query =
|
73 |
-
st.success("
|
74 |
except ValueError as e:
|
75 |
-
st.error(f"
|
|
|
6 |
from huggingface_hub import login
|
7 |
from pydantic import BaseModel, model_validator
|
8 |
|
9 |
+
# Token Secret of Hugging Face
|
10 |
huggingface_token = st.secrets["HUGGINGFACEHUB_API_TOKEN"]
|
11 |
login(huggingface_token)
|
12 |
|
13 |
+
# Load Llama 3.2
|
14 |
model_name = "meta-llama/Llama-3.2-3B-Instruct"
|
15 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
16 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
17 |
|
18 |
pipe = pipeline("feature-extraction", model=model, tokenizer=tokenizer)
|
19 |
|
20 |
+
# Use transformers pipeline
|
21 |
llm_pipeline = HuggingFacePipeline(pipeline=pipe)
|
22 |
|
23 |
+
# Interfase of Streamlit
|
24 |
+
st.title("Cosine Similarity with Llama 3.1")
|
25 |
|
26 |
|
27 |
+
# initialize query
|
28 |
query = ""
|
29 |
|
30 |
+
# Upload CSV file
|
31 |
+
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
32 |
|
33 |
|
34 |
if uploaded_file is not None:
|
35 |
+
# Read CSV file
|
36 |
df = pd.read_csv(uploaded_file)
|
37 |
|
38 |
+
# Request a query to user
|
39 |
+
query = st.text_input("Enter your query:")
|
40 |
|
41 |
if query:
|
42 |
+
# Create the prompt to calculate cosine similarity score
|
43 |
prompt = f"""
|
44 |
+
With the 'job_title' column from the CSV, you need to calculate the cosine similarity score between each record and the query.
|
45 |
+
You should create a new column called 'Score' with those scores and then sort the records from highest to lowest 'Score'.
|
46 |
+
The query is: {query}
|
47 |
"""
|
48 |
|
49 |
+
# Call the model with the prompt
|
50 |
try:
|
51 |
response = llm_pipeline(prompt)
|
52 |
+
# Show Model answer
|
53 |
+
st.write("Model Answer:", response)
|
54 |
|
55 |
except Exception as e:
|
56 |
+
st.error(f"Error while processing: {str(e)}")
|
57 |
|
58 |
+
# Data validation Pydantic
|
59 |
+
class ConsultModel(BaseModel):
|
60 |
query: str
|
61 |
|
62 |
@model_validator(mode='before')
|
63 |
+
def validate_query(cls, values):
|
64 |
query = values.get('query')
|
65 |
if not query:
|
66 |
+
raise ValueError("Query cannot be empty.")
|
67 |
return values
|
68 |
|
69 |
+
# Example
|
70 |
if query:
|
71 |
try:
|
72 |
+
valid_query = ConsultModel(query=query)
|
73 |
+
st.success("Valid consult.")
|
74 |
except ValueError as e:
|
75 |
+
st.error(f"Validation error: {e}")
|