File size: 3,864 Bytes
b43dd91 a16a609 |
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 |
import streamlit as st
from audiorecorder import audiorecorder
import os
import openai
import requests
import re
def create_recipe(path):
import whisper
# small model for transcription
model = whisper.load_model("base")
result = model.transcribe("audio.wav")
# print(result["text"])
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", # engine options = ["gpt-35-16k", "gpt-4", "gpt-4-32k"]
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:'"},
],
)
# print(response)
# print(response["choices"][0]["message"]["content"])
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"
# Assign the API version (DALL-E is currently supported for the 2023-06-01-preview API version only)
openai.api_version = "2023-06-01-preview"
openai.api_type = "azure"
pattern = re.compile(r'Recipe:(.*?)Ingredients:', re.DOTALL)
# Use re.search() to find the matching portion of the text.
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 # Enter your prompt text here
)
# Set the directory for the stored image
image_dir = os.path.join(os.curdir, "images")
# If the directory doesn't exist, create it
if not os.path.isdir(image_dir):
os.mkdir(image_dir)
# Initialize the image path (note the filetype should be png)
image_path = os.path.join(image_dir, "generated_image.png")
# # Retrieve the generated image
image_url = generation_response["data"][0]["url"] # extract image URL from response
generated_image = requests.get(image_url).content # download the image
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.title("Cooking with a Dash of AI: Recipe Generator Delivers Delicious Delights!")
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:
# To play audio in frontend:
# st.audio(audio.export().read())
# # To save audio to a file, use pydub export method:
audio.export("audio.wav", format="wav")
recipe_text = create_recipe("audio.wav")
create_recipe_image(recipe_text)
# To get audio properties, use pydub AudioSegment properties:
# st.write(f"Frame rate: {audio.frame_rate}, Frame width: {audio.frame_width}, Duration: {audio.duration_seconds} seconds") |