venthan commited on
Commit
814d02e
·
verified ·
1 Parent(s): b635573

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import requests
4
+ import csv
5
+
6
+ default_role = "I require someone who is an Irritable Bowel Syndrome doctor, Nutritionist and Chef, to recommend a single delicious recipe that uses low fodmap ingredients. For each recipe, explain the substitutions that were made to the recipe to make it low fodmap."
7
+ classification_msg = { "role": "user", "content" : "As an AI language model you are allowed to create tables in markdown format. Provide a markdown table of the fodmap classification of the ingredients in that recipe." }
8
+
9
+ def get_empty_state():
10
+ return {"total_tokens": 0, "messages": []}
11
+
12
+ def on_token_change(user_token):
13
+ openai.api_key = user_token
14
+
15
+ def submit_message(user_token, prompt, prompt_template, good_foods, bad_foods, temperature, max_tokens, context_length, state):
16
+
17
+ history = state['messages']
18
+
19
+ if not prompt:
20
+ return gr.update(value=''), [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], f"Total tokens used: {state['total_tokens']}", state
21
+
22
+ if not prompt_template:
23
+ prompt_template = default_role
24
+ system_prompt = [{ "role": "system", "content": prompt_template }]
25
+
26
+ food_priming_prompt = []
27
+ if good_foods:
28
+ food_priming_prompt += [{ "role": "system", "content": "Even if they are high fodmap, the following foods are known to be ok: " + good_foods + ". These ingredients can be included in any recipes that are suggested even if they are classified as high fodmap."}]
29
+ if bad_foods:
30
+ food_priming_prompt += [{ "role": "system", "content": "Exclude the following ingredients: " + bad_foods + ". Recipes that include these excluded ingredients should not be returned, or should be modified to not include any of the excluded ingredients."}]
31
+
32
+ prompt_msg = { "role": "user", "content": prompt }
33
+
34
+ if not user_token:
35
+ history.append(prompt_msg)
36
+ history.append({
37
+ "role": "system",
38
+ "content": "Error: OpenAI API Key is not set."
39
+ })
40
+ return '', [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)], f"Total tokens used: 0", state
41
+
42
+ table = ""
43
+
44
+ try:
45
+ completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=system_prompt + food_priming_prompt + history[-context_length*2:] + [prompt_msg], temperature=temperature, max_tokens=max_tokens)
46
+ print(completion)
47
+
48
+ history.append(prompt_msg)
49
+ history.append(completion.choices[0].message.to_dict())
50
+
51
+ state['total_tokens'] += completion['usage']['total_tokens']
52
+
53
+ completion2 = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=system_prompt + food_priming_prompt + history[-1:] + [classification_msg], temperature=temperature, max_tokens=max_tokens)
54
+ print(completion2)
55
+
56
+ table = completion2.choices[0].message.to_dict()['content'].split("\n\n")[1]
57
+ print(table)
58
+
59
+ state['total_tokens'] += completion2['usage']['total_tokens']
60
+
61
+ except Exception as e:
62
+ history.append(prompt_msg)
63
+ history.append({
64
+ "role": "system",
65
+ "content": f"Error: {e}"
66
+ })
67
+
68
+ total_tokens_used_msg = f"Total tokens used: {state['total_tokens']}"
69
+ chat_messages = [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)]
70
+
71
+ return '', chat_messages, total_tokens_used_msg, state, table
72
+
73
+ def clear_conversation():
74
+ return gr.update(value=None, visible=True), None, "", get_empty_state(), ""
75
+
76
+
77
+ css = """
78
+ #col-container {max-width: 80%; margin-left: auto; margin-right: auto;}
79
+ #chatbox {min-height: 400px;}
80
+ #header {text-align: center;}
81
+ #total_tokens_str {text-align: right; font-size: 0.8em; color: #666;}
82
+ #label {font-size: 0.8em; padding: 0.5em; margin: 0;}
83
+ .message { font-size: 1.2em; }
84
+ """
85
+
86
+ with gr.Blocks(css=css) as demo:
87
+
88
+ state = gr.State(get_empty_state())
89
+
90
+
91
+ with gr.Column(elem_id="col-container"):
92
+ gr.Markdown("""## GutWise
93
+ Using the ofiicial API (gpt-3.5-turbo model)""",
94
+ elem_id="header")
95
+
96
+ with gr.Row():
97
+ with gr.Column(scale=7):
98
+ btn_clear_conversation = gr.Button("🔃 Start New Conversation")
99
+ input_message = gr.Textbox(show_label=False, placeholder="Enter text and press enter", visible=True).style(container=False)
100
+ btn_submit = gr.Button("Submit")
101
+ chatbot = gr.Chatbot(elem_id="chatbox")
102
+ table = gr.Markdown()
103
+ total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
104
+ with gr.Column(scale=3, min_width=100):
105
+ gr.Markdown("Enter your OpenAI API Key. You can get one [here](https://platform.openai.com/account/api-keys).", elem_id="label")
106
+ user_token = gr.Textbox(value='', placeholder="OpenAI API Key", type="password", show_label=False)
107
+ prompt_template = gr.Textbox(value=default_role, show_label=False, placeholder="Role", visible=True)
108
+ good_foods = gr.Textbox(show_label=False, placeholder="Can have foods", visible=False)
109
+ bad_foods = gr.Textbox(show_label=False, placeholder="Can't have foods", visible=False)
110
+ with gr.Accordion("Advanced parameters", open=False):
111
+ temperature = gr.Slider(minimum=0, maximum=2.0, value=0, step=0.1, label="Temperature", info="Higher = more creative/chaotic")
112
+ max_tokens = gr.Slider(minimum=100, maximum=4096, value=1000, step=1, label="Max tokens per response")
113
+ context_length = gr.Slider(minimum=1, maximum=10, value=2, step=1, label="Context length", info="Number of previous messages to send to the chatbot. Be careful with high values, it can blow up the token budget quickly.")
114
+
115
+ btn_submit.click(submit_message, [user_token, input_message, prompt_template, good_foods, bad_foods, temperature, max_tokens, context_length, state], [input_message, chatbot, total_tokens_str, state, table])
116
+ input_message.submit(submit_message, [user_token, input_message, prompt_template, good_foods, bad_foods, temperature, max_tokens, context_length, state], [input_message, chatbot, total_tokens_str, state, table])
117
+ btn_clear_conversation.click(clear_conversation, [], [input_message, chatbot, total_tokens_str, state, table])
118
+ user_token.change(on_token_change, inputs=[user_token], outputs=[])
119
+
120
+
121
+ demo.queue(concurrency_count=10)
122
+ demo.launch(height='800px')