|
import streamlit as st |
|
from audiorecorder import audiorecorder |
|
import os |
|
import openai |
|
import requests |
|
import re |
|
def create_recipe(path): |
|
import whisper |
|
|
|
model = whisper.load_model("base") |
|
result = model.transcribe("audio.wav") |
|
|
|
openai.api_type = "azure" |
|
openai.api_version = "2023-05-15" |
|
openai.api_base = "https://futurice-data-day-2023.openai.azure.com/" |
|
openai.api_key = "d2cb77316cee4feb9d96d70ed77ef27d" |
|
response = openai.ChatCompletion.create( |
|
engine="gpt-35-16k", |
|
messages=[ |
|
{"role": "system", "content": "You are a Recipe generator."}, |
|
{"role": "user", "content": result["text"]}, |
|
{"role": "assistant", "content": "Create a recipe based on user's dietary restriction and personalize it. Write the recipe name after word 'Recipe:' and then give the ingredients after word 'Ingredients:' and instruction after word 'Instruction:'"}, |
|
], |
|
) |
|
|
|
|
|
recipe_text = response["choices"][0]["message"]["content"] |
|
return recipe_text |
|
def create_recipe_image(recipe_text): |
|
openai.api_base = "https://futurice-data-day-2023.openai.azure.com/" |
|
openai.api_key = "d2cb77316cee4feb9d96d70ed77ef27d" |
|
|
|
openai.api_version = "2023-06-01-preview" |
|
openai.api_type = "azure" |
|
pattern = re.compile(r'Recipe:(.*?)Ingredients:', re.DOTALL) |
|
|
|
match = pattern.search(recipe_text) |
|
if match: |
|
extracted_text = match.group(1).strip() |
|
generation_response = openai.Image.create( |
|
prompt=extracted_text, size="1024x1024", n=2 |
|
) |
|
|
|
image_dir = os.path.join(os.curdir, "images") |
|
|
|
if not os.path.isdir(image_dir): |
|
os.mkdir(image_dir) |
|
|
|
image_path = os.path.join(image_dir, "generated_image.png") |
|
|
|
image_url = generation_response["data"][0]["url"] |
|
generated_image = requests.get(image_url).content |
|
with open(image_path, "wb") as image_file: |
|
image_file.write(generated_image) |
|
from PIL import Image |
|
image = Image.open(image_path) |
|
st.image(image, caption='Recipe') |
|
st.write(recipe_text) |
|
st.markdown(""" <style> .font { |
|
font-size:50px ; font-family: 'Cooper Black'; color: #FF9633;} |
|
</style> """, unsafe_allow_html=True) |
|
st.markdown('<p class="font">Cooking with a Dash of AI: Recipe Generator Delivers Delicious Delights!</p>', unsafe_allow_html=True) |
|
|
|
st.markdown(""" <style> .font2 { |
|
font-size:30px ; font-family: 'Cooper Black'; color: #000000;} |
|
</style> """, unsafe_allow_html=True) |
|
st.markdown('<p class="font2">Could you please share your favorite dish and any dietary constraints you have?</p>', unsafe_allow_html=True) |
|
audio = audiorecorder("Click to record your voice", "Click to stop recording") |
|
if len(audio) > 0: |
|
|
|
|
|
|
|
audio.export("audio.wav", format="wav") |
|
recipe_text = create_recipe("audio.wav") |
|
create_recipe_image(recipe_text) |
|
|
|
|