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