Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +160 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.chat_models import ChatOpenAI
|
2 |
+
from langchain.schema import HumanMessage, SystemMessage
|
3 |
+
|
4 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
5 |
+
from ai71 import AI71
|
6 |
+
import gradio as gr
|
7 |
+
import openai
|
8 |
+
import os
|
9 |
+
import re
|
10 |
+
import matplotlib.pyplot as plt
|
11 |
+
|
12 |
+
from PIL import Image
|
13 |
+
import numpy as np
|
14 |
+
import pytesseract
|
15 |
+
# Make sure to import the necessary OpenAI API client and configure it.
|
16 |
+
all_cals = {}
|
17 |
+
def extract_calories_and_items(text):
|
18 |
+
# Use regular expression to find all numerical values associated with "calory" or "calories"
|
19 |
+
pattern = r'(\d+)\s*(?:calory|calories)'
|
20 |
+
matches = re.findall(pattern, text, re.IGNORECASE)
|
21 |
+
|
22 |
+
# Convert the matches to integers
|
23 |
+
calories = [int(match) for match in matches]
|
24 |
+
|
25 |
+
return calories
|
26 |
+
|
27 |
+
def plot_calories(calories):
|
28 |
+
labels = sorted(calories, key=calories.get)
|
29 |
+
vals = [calories[label] for label in labels]
|
30 |
+
plt.barh(labels, vals, color='skyblue')
|
31 |
+
plt.xlabel('Calories')
|
32 |
+
plt.title('Item and Count')
|
33 |
+
plt.tight_layout()
|
34 |
+
|
35 |
+
def parse_items(items_string):
|
36 |
+
# Remove square brackets and split by comma
|
37 |
+
items_list = items_string.strip('[]').split(',')
|
38 |
+
|
39 |
+
item_dict = {}
|
40 |
+
|
41 |
+
# Define the pattern to match the quantity and item
|
42 |
+
pattern = r'(\d+)\s*x\s*(\w+)'
|
43 |
+
|
44 |
+
for item in items_list:
|
45 |
+
match = re.match(pattern, item.strip())
|
46 |
+
if match:
|
47 |
+
quantity = int(match.group(1))
|
48 |
+
item_name = match.group(2)
|
49 |
+
if item_name in item_dict:
|
50 |
+
item_dict[item_name] += quantity
|
51 |
+
else:
|
52 |
+
item_dict[item_name] = quantity
|
53 |
+
|
54 |
+
return item_dict
|
55 |
+
|
56 |
+
# Set the API key for AI71
|
57 |
+
AI71_API_KEY = "key"
|
58 |
+
AI71_BASE_URL = "https://api.ai71.ai/v1/"
|
59 |
+
client = AI71(AI71_API_KEY)
|
60 |
+
|
61 |
+
search = DuckDuckGoSearchRun()
|
62 |
+
|
63 |
+
# usr_input = input(f"User:")
|
64 |
+
#
|
65 |
+
# print(items)
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
def chatGPT_food(userinput, temperature=0.1, max_tokens=300):
|
70 |
+
|
71 |
+
keyword = client.chat.completions.create(
|
72 |
+
model="tiiuae/falcon-180B-chat",
|
73 |
+
messages=[
|
74 |
+
{"role": "system", "content": '''you need to extract the food item from the user text without any comments
|
75 |
+
example:
|
76 |
+
user: I ate two apples
|
77 |
+
assistant: 2 x apple'''},
|
78 |
+
{"role": "user", "content": userinput}
|
79 |
+
],
|
80 |
+
# temperature=0.5,
|
81 |
+
)
|
82 |
+
|
83 |
+
items = parse_items(keyword.choices[0].message.content)
|
84 |
+
|
85 |
+
for item, count in items.items():
|
86 |
+
result = search.invoke(f'calories of {item}')
|
87 |
+
|
88 |
+
response = client.chat.completions.create(
|
89 |
+
model="tiiuae/falcon-180B-chat",
|
90 |
+
messages=[
|
91 |
+
{"role": "system", "content": '''based on the provided information extract the calories count per portion of the item provided, just the calories and portion in grams or ml without further comments
|
92 |
+
Example:
|
93 |
+
orange 47 calories per 100 gram
|
94 |
+
cola 38 calories per 100 gram
|
95 |
+
do not generate more or add any unneeded comments, just follow the examples strictly'''},
|
96 |
+
{"role": "user", "content": result}
|
97 |
+
],
|
98 |
+
temperature=0.2,
|
99 |
+
)
|
100 |
+
|
101 |
+
# print("search")
|
102 |
+
# print(result)
|
103 |
+
# print("ai")
|
104 |
+
# print (response.choices[0].message.content)
|
105 |
+
calories = extract_calories_and_items(response.choices[0].message.content)
|
106 |
+
# print("calories")
|
107 |
+
# print(calories)
|
108 |
+
try:
|
109 |
+
all_cals[f"{count}x{item}"] = count*calories[0]
|
110 |
+
except:
|
111 |
+
continue
|
112 |
+
return all_cals
|
113 |
+
|
114 |
+
def chatGPT_invoice(userinput, temperature=0.1, max_tokens=300):
|
115 |
+
response = client.chat.completions.create(
|
116 |
+
model="tiiuae/falcon-180B-chat",
|
117 |
+
messages=[
|
118 |
+
{"role": "system", "content": '''from the following invoice, find the name of the restaurant, then write a table for each food in the invoice and estimate its calories count only knowing that this food is from the same restaurant, with no further text or comments, or notes:
|
119 |
+
example:
|
120 |
+
"Restaurant: KFC
|
121 |
+
<insert the table of food and estimated calories>"
|
122 |
+
Do it for this text:'''},
|
123 |
+
{"role": "user", "content": userinput}
|
124 |
+
],
|
125 |
+
temperature=temperature,
|
126 |
+
max_tokens=max_tokens
|
127 |
+
)
|
128 |
+
return response.choices[0].message.content
|
129 |
+
|
130 |
+
def update_plot(userinput):
|
131 |
+
# all_cals = chatGPT_food(userinput)
|
132 |
+
fig, ax = plt.subplots()
|
133 |
+
plot_calories(all_cals)
|
134 |
+
return fig
|
135 |
+
|
136 |
+
def ocr(input_img):
|
137 |
+
img1 = np.array(input_img)
|
138 |
+
text = pytesseract.image_to_string(img1)
|
139 |
+
output = chatGPT_invoice(text)
|
140 |
+
return output
|
141 |
+
|
142 |
+
with gr.Blocks() as demo:
|
143 |
+
with gr.Tab("Food Calories"):
|
144 |
+
food = gr.Textbox(label="Food")
|
145 |
+
output = gr.Textbox(label="Calories")
|
146 |
+
greet_btn = gr.Button("Get Calories")
|
147 |
+
greet_btn.click(fn=chatGPT_food, inputs=food, outputs=output)
|
148 |
+
|
149 |
+
with gr.Tab("Invoice OCR"):
|
150 |
+
image_input = gr.Image(height=200, width=200)
|
151 |
+
output_text = gr.Textbox(label="Estimated Calories from Invoice")
|
152 |
+
demo_ocr = gr.Interface(fn=ocr, inputs=image_input, outputs=output_text)
|
153 |
+
|
154 |
+
with gr.Tab("Calories Plot"):
|
155 |
+
# food_plot = gr.Textbox(label="Enter Food for Plot")
|
156 |
+
plot_output = gr.Plot(label="Calories Plot")
|
157 |
+
plot_btn = gr.Button("Generate Plot")
|
158 |
+
plot_btn.click(fn=update_plot, inputs=plot_btn, outputs=plot_output)
|
159 |
+
|
160 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
langchain
|
3 |
+
gradio
|
4 |
+
ai71
|
5 |
+
pytesseract
|
6 |
+
matplotlib
|
7 |
+
langchain-community
|