root-sajjan
commited on
llm
Browse files- llm/fridge.JPG +0 -0
- llm/inference.py +110 -0
- llm/upload_image.py +39 -0
llm/fridge.JPG
ADDED
llm/inference.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import InferenceClient
|
2 |
+
import nltk
|
3 |
+
import re
|
4 |
+
import requests
|
5 |
+
|
6 |
+
nltk.download('punkt')
|
7 |
+
nltk.download('punkt_tab')
|
8 |
+
nltk.download('averaged_perceptron_tagger')
|
9 |
+
|
10 |
+
|
11 |
+
client = InferenceClient(api_key="xyz")
|
12 |
+
|
13 |
+
|
14 |
+
def extract_product_info(text):
|
15 |
+
# Initialize result dictionary
|
16 |
+
result = {"brand": None, "model": None, "description": None, "price": None}
|
17 |
+
|
18 |
+
# Extract price separately using regex (to avoid confusion with brand name)
|
19 |
+
price_match = re.search(r'\$\s?\d{1,3}(?:,\d{3})*(?:\.\d{2})?', text)
|
20 |
+
if price_match:
|
21 |
+
result["price"] = price_match.group().replace("$", "").replace(",", "").strip()
|
22 |
+
# Remove the price part from the text to prevent it from being included in the brand/model extraction
|
23 |
+
text = text.replace(price_match.group(), "").strip()
|
24 |
+
|
25 |
+
# Tokenize the remaining text and tag parts of speech
|
26 |
+
tokens = nltk.word_tokenize(text)
|
27 |
+
pos_tags = nltk.pos_tag(tokens)
|
28 |
+
|
29 |
+
# Extract brand and model (Proper Nouns + Alphanumeric patterns)
|
30 |
+
brand_parts = []
|
31 |
+
model_parts = []
|
32 |
+
description_parts = []
|
33 |
+
|
34 |
+
# First part: Extract brand and model info
|
35 |
+
for word, tag in pos_tags:
|
36 |
+
if tag == 'NNP' or re.match(r'[A-Za-z0-9-]+', word):
|
37 |
+
if len(brand_parts) == 0: # Assume the first proper noun is the brand
|
38 |
+
brand_parts.append(word)
|
39 |
+
else: # Model number tends to follow the brand
|
40 |
+
model_parts.append(word)
|
41 |
+
else:
|
42 |
+
description_parts.append(word)
|
43 |
+
|
44 |
+
# Assign brand and model to result dictionary
|
45 |
+
if brand_parts:
|
46 |
+
result["brand"] = " ".join(brand_parts)
|
47 |
+
if model_parts:
|
48 |
+
result["model"] = " ".join(model_parts)
|
49 |
+
|
50 |
+
# Combine the remaining parts as description
|
51 |
+
result["description"] = " ".join(description_parts)
|
52 |
+
|
53 |
+
return result
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
def extract_info(text):
|
58 |
+
API_URL = "https://api-inference.huggingface.co/models/google/flan-t5-large"
|
59 |
+
headers = {"Authorization": "Bearer hf_xyz"}
|
60 |
+
payload = {"inputs": f"From the given text, extract brand name, model number, description about it, and its average price in today's market. Give me back a python dictionary with keys as brand_name, model_number, desc, price. The text is {text}.",}
|
61 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
62 |
+
print('GOOGLEE LLM OUTPUTTTTTTT\n\n',response )
|
63 |
+
output = response.json()
|
64 |
+
print(output)
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
def get_name(url, object):
|
69 |
+
messages = [
|
70 |
+
{
|
71 |
+
"role": "user",
|
72 |
+
"content": [
|
73 |
+
{
|
74 |
+
"type": "text",
|
75 |
+
"text": f"Is this a {object}?. Can you guess what it is and give me the closest brand it resembles to? or a model number? And give me its average price in today's market in USD. In output, give me its normal name, model name, model number and price. separated by commas. No description is needed."
|
76 |
+
},
|
77 |
+
{
|
78 |
+
"type": "image_url",
|
79 |
+
"image_url": {
|
80 |
+
"url": url
|
81 |
+
}
|
82 |
+
}
|
83 |
+
]
|
84 |
+
}
|
85 |
+
]
|
86 |
+
|
87 |
+
completion = client.chat.completions.create(
|
88 |
+
model="meta-llama/Llama-3.2-11B-Vision-Instruct",
|
89 |
+
messages=messages,
|
90 |
+
max_tokens=500
|
91 |
+
)
|
92 |
+
|
93 |
+
|
94 |
+
print(f'\n\nNow output of LLM:\n')
|
95 |
+
llm_result = completion.choices[0].message['content']
|
96 |
+
print(llm_result)
|
97 |
+
print(f'\n\nThat is the output')
|
98 |
+
|
99 |
+
result = extract_product_info(llm_result)
|
100 |
+
print(f'\n\nResult brand and price:{result}')
|
101 |
+
|
102 |
+
# result2 = extract_info(llm_result)
|
103 |
+
# print(f'\n\nFrom Google llm:{result2}')
|
104 |
+
|
105 |
+
return result
|
106 |
+
|
107 |
+
# url = "https://i.ibb.co/mNYvqDL/crop_39.jpg"
|
108 |
+
# object="fridge"
|
109 |
+
|
110 |
+
# get_name(url, object)
|
llm/upload_image.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
def upload_image_to_imgbb(image_path, api_key="0e7fb6d183b8db925812dee230f71079"):
|
4 |
+
"""
|
5 |
+
Uploads an image to ImgBB and returns the URL.
|
6 |
+
|
7 |
+
:param image_path: Path to the local image
|
8 |
+
:param api_key: ImgBB API key
|
9 |
+
:return: URL of the uploaded image
|
10 |
+
"""
|
11 |
+
try:
|
12 |
+
# API endpoint for ImgBB
|
13 |
+
url = "https://api.imgbb.com/1/upload"
|
14 |
+
|
15 |
+
# Open the image in binary mode
|
16 |
+
with open(image_path, "rb") as image_file:
|
17 |
+
# Send POST request to upload the image
|
18 |
+
response = requests.post(
|
19 |
+
url,
|
20 |
+
data={"key": api_key},
|
21 |
+
files={"image": image_file}
|
22 |
+
)
|
23 |
+
|
24 |
+
# Check if the request was successful
|
25 |
+
if response.status_code == 200:
|
26 |
+
data = response.json()
|
27 |
+
print(f'Uploaded to {data["data"]["url"]}')
|
28 |
+
return data["data"]["url"]
|
29 |
+
else:
|
30 |
+
raise Exception(f"Error uploading image: {response.status_code}, {response.text}")
|
31 |
+
except Exception as e:
|
32 |
+
return str(e)
|
33 |
+
|
34 |
+
# # Replace with your local image path and ImgBB API key
|
35 |
+
# image_path = "fridge.JPG" # Replace this with your local image path
|
36 |
+
# api_key = "0e7fb6d183b8db925812dee230f71079" # Get your API key from https://api.imgbb.com/
|
37 |
+
|
38 |
+
# uploaded_url = upload_image_to_imgbb(image_path, api_key)
|
39 |
+
# print(f"Uploaded image URL: {uploaded_url}")
|