forked from AbdelTID/Hackathon-Multimodal-Benin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
274 lines (222 loc) · 9.84 KB
/
app.py
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import streamlit as st
from st_audiorec import st_audiorec
import replicate
# import pypiwin32pip
# from audio_recorder_streamlit import audio_recorder
# from bokeh.models.widgets import Button
# from bokeh.models import CustomJS
# from streamlit_bokeh_events import streamlit_bokeh_events
# from streamlit_mic_recorder import mic_recorder
from dotenv import load_dotenv
import os
import google.generativeai as genai
import requests
import zipfile
import time
import sys
import io
# from utils import icon
from streamlit_image_select import image_select
load_dotenv() ##load all the nevironment variables
from transformers import pipeline
# Create a speech recognition pipeline using a pre-trained model
pipe = pipeline("automatic-speech-recognition", model="chrisjay/fonxlsr")
REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")
REPLICATE_MODEL_ENDPOINTSTABILITY = os.getenv("REPLICATE_MODEL_ENDPOINTSTABILITY")
NUM_IMAGES_PER_ROW = 3
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat(history=[])
def get_gemini_response(question):
response =chat.send_message(question,stream=True)
return response
def display_chat_messages() -> None:
"""Print message history
@returns None
"""
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if "images" in message:
for i in range(0, len(message["images"]), NUM_IMAGES_PER_ROW):
cols = st.columns(NUM_IMAGES_PER_ROW)
for j in range(NUM_IMAGES_PER_ROW):
if i + j < len(message["images"]):
cols[j].image(message["images"][i + j], width=200)
st.set_page_config(page_title="Nùɖúɖú",
page_icon="",
layout="wide")
# Title
st.title("Nùɖúɖú")
with st.sidebar:
st.title("Nùɖúɖú")
st.subheader("Benin Food Chat")
st.markdown(
"""Nùɖúɖú is a chatbot built for anyone interested in exploring and enjoying the rich culinary heritage of Benin.
It offers speech to image as well as text to image generation about the local food of Benin (Atassi, Amiwo)"""
)
uploaded_file = st.file_uploader("Choose an audio file", type=['wav', 'mp3', 'aac',"opus"])
submit = st.button("Submit & Process")
st.divider()
# st.markdown(
# """
# ---
# LinkedIn → [Abdel Tidjani](https://www.linkedin.com/in/abdelanlah-tidjani/)
# LinkedIn → [Anne-Marie Atignon](www.linkedin.com/in/anne-marie-atignon/)
# LinkedIn → [Abdel Tidjani](https://www.linkedin.com/in/abdelanlah-tidjani/)
# LinkedIn → [Abdel Tidjani](https://www.linkedin.com/in/abdelanlah-tidjani/)
# LinkedIn → [Abdel Tidjani](https://www.linkedin.com/in/abdelanlah-tidjani/)
# LinkedIn → [Abdel Tidjani](https://www.linkedin.com/in/abdelanlah-tidjani/)
# """
# )
# Information
with st.expander("Built for the Benin Multimodal AI Hackathon 2024"):
st.markdown(
"""
This project is a submission for the [Benin Multimodal AI Hackathon 2024](https://lablab.ai/event/benin-multimodal-ai-hackathon).
Benin Food Chat uses
You can find the submission in this [GitHub repo](https://github.com/AbdelTID/Hackathon-Multimodal-Benin)
"""
)
st.subheader("Role")
st.markdown(
"""
- Dish Visualization: Provide users with an authentic platform to unleash their creativity and showcase their culinary ideas by accurately generating images of local dishes through AI technology.
- Recipe Sharing: Provide users with authentic recipes for traditional Beninese dishes, complete with cooking tips and ingredient lists.
- Cultural Education: Educate users about the history and cultural significance of various Beninese foods, connecting culinary practices with local traditions and festivals.
- Restaurant Recommendations: Suggest local or international restaurants that serve Beninese cuisine, potentially integrating location-based services to direct users to nearby dining options.
- Language Integration: Help non-native speakers learn culinary-related terms and phrases in local languages such as Fon or Yoruba, enhancing their dining and cooking experiences.
"""
)
col1, col2, col3 = st.columns([0.2, 0.5, 0.2])
# col2.image("./img/anim.gif")
# Initialize text-to-speech engine
# engine = pyttsx3.init()
# def speak(text: str):
# """Convert text to speech and return audio file."""
# engine.save_to_file(text, 'speech.mp3')
# engine.runAndWait()
# with open("speech.mp3", "rb") as audio_file:
# audio_data = audio_file.read()
# return audio_data
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
st.session_state.greetings = False
# Display chat messages from history on app rerun
display_chat_messages()
# Greet user
if not st.session_state.greetings:
with st.chat_message("assistant"):
intro = "Hey! I am Nùɖúɖú, your assistant for exploring and enjoying traditional Beninese dishes, Let's get started!"
st.markdown(intro)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": intro})
st.session_state.greetings = True
# Example prompts
example_prompts = [
"Benin dish Amiwo",
"an image of atassi",
"an illustration of Benin dish amiwo",
]
example_prompts_help = [
"Food photography",
"Editorial Photography, Photography, Shot on 70mm lens, Depth of Field",
"White Balance, 32k, Super-Resolution, white background",
]
button_cols = st.columns(3)
# button_cols_2 = st.columns(3)
button_pressed = ""
if submit:
with st.chat_message("user"):
st.audio(uploaded_file.read(), format='audio/wav')
print(uploaded_file)
transcription = pipe(uploaded_file.read())
print("Transcription:", transcription['text'])
button_pressed=transcription
if button_cols[0].button(example_prompts[0]):
button_pressed = example_prompts[0]
elif button_cols[1].button(example_prompts[1]):
button_pressed = example_prompts[1]
elif button_cols[2].button(example_prompts[2]):
button_pressed = example_prompts[2]
def main_func():
input1=prompt+" benin dish recipe"
response=get_gemini_response(input1)
st.subheader("The Recipe is")
for chunk in response:
print(st.write(chunk.text))
print("_"*80)
# st.write(chat.history)
# Only call the API if the "Submit" button was pressed
if prompt is not None :
# Calling the replicate API to get the image
with generated_images_placeholder.container():
all_images = [] # List to store all generated images
output = replicate.run(
model_rep,
input={
# "seed": 13,
"width": 512,
"height": 512,
"prompt": prompt,
"refine": "no_refiner",
"scheduler": "K_EULER",
"lora_scale": 0.6,
"num_outputs": 1,
"guidance_scale": 7.5,
"apply_watermark": True,
"high_noise_frac": 0.8,
"negative_prompt": "the absolute worst quality, distorted features",
"prompt_strength": 0.8,
"num_inference_steps": 50
}
)
print(output)
if output:
st.toast(
'Your image has been generated!', icon='😍')
# Save generated image to session state
st.session_state.generated_image = output
# Displaying the image
for image in st.session_state.generated_image:
with st.container():
st.image(image, caption="Generated Image 🎈", use_column_width=False)
status.update(label="✅ Images generated!",state="complete", expanded=False)
st.session_state.messages.append(
{"role": "assistant", "content": response["text"], "images": image}
)
st.experimental_rerun()
# wav_audio_data = st_audiorec() # tadaaaa! yes, that's it! :D
# if wav_audio_data is not None:
# with st.chat_message("user"):
# st.audio(wav_audio_data, format='audio/wav')
if prompt := (st.chat_input("What dish are you looking for? Express your mind!") or button_pressed ):
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
prompt = prompt.replace('"', "").replace("'", "").lower()
# Placeholders for images and gallery
generated_images_placeholder = st.empty()
# gallery_placeholder = st.empty() # will be add later
images = []
if prompt != "":
model_rep = ""
if "amiwo" in prompt or "ami wo" in prompt or "ami wɔ" in prompt:
model_rep= "abdeltid/foodlocalbenin:e0868ed242d6478a91aa7fc4dc92917f1856a60c6c4c708f83b0d8ea98dffac1"
elif "atassi" in prompt or "atasi"in prompt:
model_rep= "abdeltid/foodlocalbenin:3fd0bae00e1f3d348df7768045f6e47ab90fb37bef7f82d45aa2f0f41c591313"
# query = prompt.strip().lower()
with st.status('👩🏾🍳 Whipping up your words into art...', expanded=True) as status:
st.write("⚙️ Model initiated")
st.write("🙆♀️ Stand up and strecth in the meantime")
try:
main_func()
except Exception as e:
print(e)
st.error(f'Encountered an error: {e}', icon="🚨")
# If not submitted, chill here 🍹
else:
pass