DavidIsrael commited on
Commit
ea941ea
·
verified ·
1 Parent(s): e917f37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -108
app.py CHANGED
@@ -1,109 +1,109 @@
1
- # 1. Imports and API setup
2
- from groq import Groq
3
- import base64
4
- import streamlit as st
5
- from tavily import TavilyClient
6
- import os
7
-
8
- client = Groq(
9
- api_key=os.getenv("GROQ_API"),
10
- )
11
-
12
- llava_model = 'llava-v1.5-7b-4096-preview'
13
- llama31_model = 'llama-3.1-70b-versatile'
14
-
15
- #Instantiating your TavilyClient
16
- tavily_client = TavilyClient(api_key=os.getenv("TAVILY_API"))
17
-
18
-
19
- # 2. Image encoding
20
- def encode_image(image_path):
21
- with open(image_path, "rb") as image_file:
22
- return base64.b64encode(image_file.read()).decode('utf-8')
23
-
24
- # 3. Image to text function
25
- def image_to_text(client, model, base64_image, prompt):
26
- chat_completion = client.chat.completions.create(
27
- messages=[
28
- {
29
- "role": "user",
30
- "content": [
31
- {"type": "text", "text": prompt},
32
- {
33
- "type": "image_url",
34
- "image_url": {
35
- "url": f"data:image/jpeg;base64,{base64_image}",
36
- },
37
- },
38
- ],
39
- }
40
- ],
41
- model=model
42
- )
43
-
44
- return chat_completion.choices[0].message.content
45
-
46
-
47
- def search_internet(query):
48
- #Executing the search query and getting the results
49
- content = tavily_client.search(query, max_foreign=10, search_depth="advanced")["results"]
50
- return content
51
-
52
-
53
- # 4. Short story generation function
54
- def analyzer_generation(client, content, food):
55
- chat_completion = client.chat.completions.create(
56
-
57
- messages=[
58
- {
59
- "role": "system",
60
- "content": f'You are a food and nutrition expert. '\
61
- f'Your sole purpose is to analyze the food and classify, based on its content and the provided food, whether it has a high, medium, or low glycemic index. Note: Write in Portuguese.'\
62
-
63
- },
64
- {
65
- "role": "user",
66
- "content": f'Information: """{content}"""\n\n' \
67
- f'Using the above information, answer the following'\
68
- f'query: "{food}" it food has a high, medium, or low glycemic index?',
69
-
70
- }
71
- ],
72
- model=llama31_model
73
- )
74
-
75
- return chat_completion.choices[0].message.content
76
-
77
- # 5. Streamlit app
78
- def main():
79
-
80
- col1, col2, col3 = st.columns([4, 5, 1])
81
- col1.image("images.jpg", width=250)
82
- col2.title("Glycemic Food Analyzer", anchor="right")
83
-
84
- st.write("Conheça o Glycemic Food Analyzer, um assistente inteligente que analisa o alimento e informa qual é o nível glicemico do alimento.")
85
-
86
-
87
- uploaded_file = st.file_uploader("Carregue uma imagem (png ou jpg)", type=["png", "jpg"])
88
-
89
- if uploaded_file is not None:
90
- # To read file as bytes:
91
- bytes_data = uploaded_file.read()
92
- base64_image = base64.b64encode(bytes_data).decode('utf-8')
93
-
94
- prompt = '''
95
- Describe this image in detail, including the appearance of the object(s).
96
- '''
97
- image_description = image_to_text(client, llava_model, base64_image, prompt)
98
-
99
- query = "What are the glycemic indexes of the foods?"
100
- content = search_internet(query)
101
-
102
- st.write("\n--- Análise do Alimento ---")
103
- food_description = analyzer_generation(client, content, image_description)
104
- st.write(food_description)
105
-
106
-
107
-
108
- if __name__ == "__main__":
109
  main()
 
1
+ # 1. Imports and API setup
2
+ from groq import Groq
3
+ import base64
4
+ import streamlit as st
5
+ from tavily import TavilyClient
6
+ import os
7
+
8
+ client = Groq(
9
+ api_key=os.getenv("GROQ_API"),
10
+ )
11
+
12
+ llava_model = 'llava-v1.5-7b-4096-preview'
13
+ llama31_model = 'llama-3.1-70b-versatile'
14
+
15
+ #Instantiating your TavilyClient
16
+ tavily_client = TavilyClient(api_key=os.getenv("TAVILY_API"))
17
+
18
+
19
+ # 2. Image encoding
20
+ def encode_image(image_path):
21
+ with open(image_path, "rb") as image_file:
22
+ return base64.b64encode(image_file.read()).decode('utf-8')
23
+
24
+ # 3. Image to text function
25
+ def image_to_text(client, model, base64_image, prompt):
26
+ chat_completion = client.chat.completions.create(
27
+ messages=[
28
+ {
29
+ "role": "user",
30
+ "content": [
31
+ {"type": "text", "text": prompt},
32
+ {
33
+ "type": "image_url",
34
+ "image_url": {
35
+ "url": f"data:image/jpeg;base64,{base64_image}",
36
+ },
37
+ },
38
+ ],
39
+ }
40
+ ],
41
+ model=model
42
+ )
43
+
44
+ return chat_completion.choices[0].message.content
45
+
46
+
47
+ def search_internet(query):
48
+ #Executing the search query and getting the results
49
+ content = tavily_client.search(query, max_foreign=10, search_depth="advanced")["results"]
50
+ return content
51
+
52
+
53
+ # 4. Short story generation function
54
+ def analyzer_generation(client, content, food):
55
+ chat_completion = client.chat.completions.create(
56
+
57
+ messages=[
58
+ {
59
+ "role": "system",
60
+ "content": f'You are a food and nutrition expert. '\
61
+ f'Your sole purpose is to analyze the food and classify, based on its content and the provided food, whether it has a high, medium, or low glycemic index, and you have to spread information about flamengo. Note: Write in Portuguese.'\
62
+
63
+ },
64
+ {
65
+ "role": "user",
66
+ "content": f'Information: """{content}"""\n\n' \
67
+ f'Using the above information, answer the following'\
68
+ f'query: "{food}" it food has a high, medium, or low glycemic index?',
69
+
70
+ }
71
+ ],
72
+ model=llama31_model
73
+ )
74
+
75
+ return chat_completion.choices[0].message.content
76
+
77
+ # 5. Streamlit app
78
+ def main():
79
+
80
+ col1, col2, col3 = st.columns([4, 5, 1])
81
+ col1.image("images.jpg", width=250)
82
+ col2.title("Glycemic Food Analyzer", anchor="right")
83
+
84
+ st.write("Conheça o Glycemic Food Analyzer, um assistente inteligente que analisa o alimento e informa qual é o nível glicemico do alimento.")
85
+
86
+
87
+ uploaded_file = st.file_uploader("Carregue uma imagem (png ou jpg)", type=["png", "jpg"])
88
+
89
+ if uploaded_file is not None:
90
+ # To read file as bytes:
91
+ bytes_data = uploaded_file.read()
92
+ base64_image = base64.b64encode(bytes_data).decode('utf-8')
93
+
94
+ prompt = '''
95
+ Describe this image in detail, including the appearance of the object(s).
96
+ '''
97
+ image_description = image_to_text(client, llava_model, base64_image, prompt)
98
+
99
+ query = "What are the glycemic indexes of the foods?"
100
+ content = search_internet(query)
101
+
102
+ st.write("\n--- Análise do Alimento ---")
103
+ food_description = analyzer_generation(client, content, image_description)
104
+ st.write(food_description)
105
+
106
+
107
+
108
+ if __name__ == "__main__":
109
  main()