Evan Lesmez commited on
Commit
ae2bbf3
1 Parent(s): e68d711

Add rough draft of Gradio chatbot

Browse files

Manual prompting to open ChatGPT interface based on history.

Files changed (1) hide show
  1. chatbot/app.py +172 -0
chatbot/app.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain.chat_models import ChatOpenAI
3
+ from langchain.chains import ConversationChain
4
+ from langchain.memory import ConversationBufferMemory
5
+
6
+ from langchain.prompts.chat import (
7
+ HumanMessagePromptTemplate,
8
+ MessagesPlaceholder,
9
+ ChatPromptTemplate,
10
+ )
11
+ from engineer_prompt import init_prompt
12
+
13
+ # from transformers import (
14
+ # BlipProcessor,
15
+ # BlipForConditionalGeneration,
16
+ # BlipForQuestionAnswering,
17
+ # )
18
+ # import torch
19
+ # from PIL import Image
20
+
21
+ # class ImageCaptioning:
22
+ # def __init__(self, device):
23
+ # print(f"Initializing ImageCaptioning to {device}")
24
+ # self.device = device
25
+ # self.torch_dtype = torch.float16 if "cuda" in device else torch.float32
26
+ # self.processor = BlipProcessor.from_pretrained(
27
+ # "Salesforce/blip-image-captioning-base"
28
+ # )
29
+ # self.model = BlipForConditionalGeneration.from_pretrained(
30
+ # "Salesforce/blip-image-captioning-base", torch_dtype=self.torch_dtype
31
+ # ).to(self.device)
32
+
33
+ # def inference(self, image_path):
34
+ # inputs = self.processor(Image.open(image_path), return_tensors="pt").to(
35
+ # self.device, self.torch_dtype
36
+ # )
37
+ # out = self.model.generate(**inputs)
38
+ # captions = self.processor.decode(out[0], skip_special_tokens=True)
39
+ # print(
40
+ # f"\nProcessed ImageCaptioning, Input Image: {image_path}, Output Text: {captions}"
41
+ # )
42
+ # return captions
43
+
44
+
45
+ # class VisualQuestionAnswering:
46
+ # def __init__(self, device):
47
+ # print(f"Initializing VisualQuestionAnswering to {device}")
48
+ # self.torch_dtype = torch.float16 if "cuda" in device else torch.float32
49
+ # self.device = device
50
+ # self.processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
51
+ # self.model = BlipForQuestionAnswering.from_pretrained(
52
+ # "Salesforce/blip-vqa-base", torch_dtype=self.torch_dtype
53
+ # ).to(self.device)
54
+
55
+ # def inference(self, image_path, question):
56
+ # raw_image = Image.open(image_path).convert("RGB")
57
+ # inputs = self.processor(raw_image, question, return_tensors="pt").to(
58
+ # self.device, self.torch_dtype
59
+ # )
60
+ # out = self.model.generate(**inputs)
61
+ # answer = self.processor.decode(out[0], skip_special_tokens=True)
62
+ # print(
63
+ # f"\nProcessed VisualQuestionAnswering, Input Image: {image_path}, Input Question: {question}, "
64
+ # f"Output Answer: {answer}"
65
+ # )
66
+ # return
67
+
68
+
69
+ class ConversationBot:
70
+ def __init__(
71
+ self,
72
+ ):
73
+ self.chat = ChatOpenAI(temperature=1, verbose=True)
74
+ self.memory = ConversationBufferMemory(return_messages=True)
75
+ self.init_prompt_msgs = init_prompt.messages
76
+ self.ai_prompt_questions = {
77
+ "ingredients": self.init_prompt_msgs[1],
78
+ "allergies": self.init_prompt_msgs[3],
79
+ "recipe_open_params": self.init_prompt_msgs[5],
80
+ }
81
+
82
+ def respond(self, user_msg, chat_history):
83
+ response = self._get_bot_response(user_msg, chat_history)
84
+ chat_history.append((user_msg, response))
85
+ return "", chat_history
86
+
87
+ def init_conversation(self, formatted_chat_prompt):
88
+ self.conversation = ConversationChain(
89
+ llm=self.chat,
90
+ memory=self.memory,
91
+ prompt=formatted_chat_prompt,
92
+ verbose=True,
93
+ )
94
+
95
+ def reset(self):
96
+ self.memory.clear()
97
+
98
+ def _get_bot_response(self, user_msg: str, chat_history) -> str:
99
+ if len(chat_history) < 2:
100
+ return self.ai_prompt_questions["allergies"].prompt.template
101
+
102
+ if len(chat_history) < 3:
103
+ return self.ai_prompt_questions["recipe_open_params"].prompt.template
104
+
105
+ if len(chat_history) < 4:
106
+ user = 0
107
+ ai = 1
108
+ user_msgs = [msg_pair[user] for msg_pair in chat_history[1:]]
109
+ f_init_prompt = init_prompt.format_prompt(
110
+ ingredients=user_msgs[0],
111
+ allergies=user_msgs[1],
112
+ recipe_freeform_input=user_msg,
113
+ )
114
+ chat_msgs = f_init_prompt.to_messages()
115
+ results = self.chat.generate([chat_msgs])
116
+ chat_msgs.extend(
117
+ [
118
+ results.generations[0][0].message,
119
+ MessagesPlaceholder(variable_name="history"),
120
+ HumanMessagePromptTemplate.from_template("{input}"),
121
+ ]
122
+ )
123
+ open_prompt = ChatPromptTemplate.from_messages(chat_msgs)
124
+ # prepare the open conversation chain from this point
125
+ self.init_conversation(open_prompt)
126
+ return results.generations[0][0].message.content
127
+
128
+ response = self.conversation.predict(input=user_msg)
129
+ return response
130
+
131
+ # def run_image(self, image, state, txt, lang):
132
+ # image_filename = os.path.join("image", f"{str(uuid.uuid4())[:8]}.png")
133
+ # print("======>Auto Resize Image...")
134
+ # img = Image.open(image.name)
135
+ # width, height = img.size
136
+ # ratio = min(512 / width, 512 / height)
137
+ # width_new, height_new = (round(width * ratio), round(height * ratio))
138
+ # width_new = int(np.round(width_new / 64.0)) * 64
139
+ # height_new = int(np.round(height_new / 64.0)) * 64
140
+ # img = img.resize((width_new, height_new))
141
+ # img = img.convert("RGB")
142
+ # img.save(image_filename, "PNG")
143
+ # print(f"Resize image form {width}x{height} to {width_new}x{height_new}")
144
+ # description = self.models["ImageCaptioning"].inference(image_filename)
145
+ # Human_prompt = f'\nHuman: provide a figure named {image_filename}. The description is: {description}. This information helps you to understand this image, but you should use tools to finish following tasks, rather than directly imagine from my description. If you understand, say "Received". \n'
146
+ # self.memory.buffer = (
147
+ # self.agent.memory.buffer + Human_prompt + "AI: " + AI_prompt
148
+ # )
149
+ # state = state + [(f"![](file={image_filename})*{image_filename}*", AI_prompt)]
150
+ # print(
151
+ # f"\nProcessed run_image, Input image: {image_filename}\nCurrent state: {state}\n"
152
+ # f"Current Memory: {self.agent.memory.buffer}"
153
+ # )
154
+ # return state, state, f"{txt} {image_filename} "
155
+
156
+
157
+ with gr.Blocks() as demo:
158
+ bot = ConversationBot()
159
+ chatbot = gr.Chatbot(
160
+ value=[(None, bot.ai_prompt_questions["ingredients"].prompt.template)]
161
+ )
162
+
163
+ msg = gr.Textbox()
164
+ clear = gr.Button("Clear")
165
+
166
+ msg.submit(
167
+ fn=bot.respond, inputs=[msg, chatbot], outputs=[msg, chatbot], queue=False
168
+ )
169
+ clear.click(lambda: None, None, chatbot, queue=False).then(bot.reset)
170
+
171
+ if __name__ == "__main__":
172
+ demo.launch()