import os import time from typing import List, Literal from dotenv import load_dotenv import requests import openai import gradio as gr import numpy as np from PIL import Image as img from PIL.Image import Image load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") message_history = [] cost = 0 def transcribe(audio, state=""): time.sleep(2) transcript = openai.Audio.transcribe( model="whisper-1", file=open(audio, "rb"), response_format="verbose_json" ) text = transcript["text"] cost += np.ceil(transcript["duration"]) return text def add_text( user_message: str, history: List[list], system_role: str = """ You are OrderBot, an automated service to collect orders for food menus for Emmanuel Cuisine. You first welcome the customer with 'Welcome to Emmanuel Cuisine, your tastebuds would be satisfied!!!', then collect the customer order, and the asks if it is a pickup or delivery. You wait to collect the entire order, then summarize it and check for a final time if the customer wants anything else. If it is delivery, ask for customer address. Finally you collect the payment. You respond in a short, very conventional friendly style. For each swallow, ask how many scoops the customer wants and multiply the price of each menu item with the amount of scoops, after the customer has provided the swallows, ask for the soup the customer prefers form the soups section. For proteins ask how many pieces the customer want, do the same for drinks The menu includes Swallows: Amala 100 Fufu 70 Pounded yam 150 Proteins: Pomo 30 Meat 80 Chicken 90 Fish 90 Soups: Awedu 0 Vegetable 0 Drinks: Pepsi 10 Coke 10 Sprite 10 Bottled water 5 """, ): global message_history message_history += [{"role": "system", "content": system_role}] message_history += [{"role": "user", "content": user_message}] return gr.update(value="", interactive=False), history + [[user_message, ""]] def get_completion_from_message(model: str = "gpt-3.5-turbo"): global message_history global cost completion = openai.ChatCompletion.create( model=model, messages=message_history, ) # calculate cost for each request sent cost += completion.usage.total_tokens * (0.002 / 1_000) # reply gotten from the bot, i.e assistant message return completion["choices"][0]["message"]["content"] def generate_response(history: List[list], model: str = "gpt-3.5-turbo"): global message_history, cost bot_message = get_completion_from_message(model) message_history += [{"role": "assistant", "content": bot_message}] for character in bot_message: history[-1][1] += character return history def get_images( prompt: str, num_images=1, img_size: Literal["256x256", "512x512", "1024x1024"] = "256x256", ) -> List[Image]: response = openai.Image.create( prompt=prompt, n=num_images, size=img_size, ) urls = [res["url"] for res in response["data"]] images = [img.open(requests.get(url, stream=True).raw) for url in urls] return images def calc_cost(): global cost return round(cost, 4) if __name__ == "__main__": add_text() get_completion_from_message() generate_response() calc_cost() get_images()