File size: 3,456 Bytes
2d11cde
33bc31b
7baaae1
032e2a4
7baaae1
 
2d11cde
032e2a4
33bc31b
2d11cde
7baaae1
 
 
33bc31b
2d11cde
33bc31b
2d11cde
 
 
 
 
7baaae1
33bc31b
 
 
 
 
 
 
 
 
 
7baaae1
33bc31b
2d11cde
94a55c6
7baaae1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94a55c6
2d11cde
7baaae1
 
2d11cde
7baaae1
2d11cde
7baaae1
 
 
 
2d11cde
 
 
 
 
 
7baaae1
2d11cde
 
7baaae1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d11cde
7baaae1
2d11cde
 
 
 
 
 
 
 
 
7baaae1
2d11cde
 
7baaae1
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
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()