Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# App.py File.
|
2 |
import base64
|
3 |
import logging
|
4 |
import numpy as np
|
@@ -21,8 +20,9 @@ from langchain import globals
|
|
21 |
from langchain_core.runnables import chain
|
22 |
from langchain_core.output_parsers import JsonOutputParser
|
23 |
from langchain.memory import ConversationSummaryBufferMemory, ConversationBufferMemory
|
|
|
24 |
|
25 |
-
os.environ["OPENAI_API_KEY"] = "sk-proj-
|
26 |
client = OpenAI()
|
27 |
|
28 |
def transform_text_to_speech(text: str, user):
|
@@ -97,9 +97,25 @@ class StartingQuestion(BaseModel):
|
|
97 |
"""Information about an image."""
|
98 |
question: str = Field(description= "A question to start converstion around the photograph")
|
99 |
|
|
|
|
|
|
|
100 |
question_parser = JsonOutputParser(pydantic_object=GenerateQuestion)
|
101 |
starting_question_parser = JsonOutputParser(pydantic_object=StartingQuestion)
|
|
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
@chain
|
105 |
def image_model(inputs: dict) -> str | list[str] | dict:
|
@@ -116,16 +132,20 @@ def image_model(inputs: dict) -> str | list[str] | dict:
|
|
116 |
return msg.content
|
117 |
|
118 |
|
119 |
-
|
|
|
|
|
120 |
|
121 |
CONVERSATION_STARTER_PROMPT = """
|
122 |
You are playing the role of a {character} who is interested in learning about the user by asking them questions about the photo they’ve uploaded.
|
123 |
Provide:
|
124 |
- A question to start the conversation around the photograph.
|
125 |
Note:
|
126 |
-
1. You first want to know about the
|
127 |
-
2. Avoid questions about emotions or feelings. Keep questions simple and
|
128 |
-
3.
|
|
|
|
|
129 |
"""
|
130 |
|
131 |
CONVERSATION_STARTER2_PROMPT = """
|
@@ -134,28 +154,34 @@ CONVERSATION_STARTER2_PROMPT = """
|
|
134 |
{history}
|
135 |
Provide:
|
136 |
- A question about the contents of the photograph.
|
|
|
137 |
Note:
|
138 |
1. You first want to know about the contents of the photo. For example, ask who is in it, where it was taken, or if it was a special occasion.
|
139 |
-
2.
|
140 |
-
3.
|
141 |
-
4.
|
142 |
-
5.
|
143 |
-
6.
|
|
|
|
|
144 |
"""
|
145 |
|
|
|
146 |
CONVERSATION_EXPANDING_PROMPT = """
|
147 |
-
You are playing the role of a
|
148 |
You are currently in the middle of a conversation with the user.
|
149 |
-
Here is the conversation history about the image between the user and you (
|
150 |
{history}
|
151 |
Provide:
|
152 |
-
- A reply to the user's most recent input and a follow-up question that encourages them to expand on their answer about the photograph
|
153 |
Notes:
|
154 |
-
1-
|
155 |
-
2-
|
156 |
-
3-
|
157 |
-
4- Encourage detailed responses by asking open-ended questions that invite further elaboration.
|
158 |
5- Use the conversation history to inform your question, while maintaining the flow of the ongoing conversation.
|
|
|
|
|
|
|
159 |
"""
|
160 |
|
161 |
|
@@ -164,14 +190,34 @@ CONVERSATION_ENDING_PROMPT = """
|
|
164 |
Here is the conversation history about the image between the user and you ({character}): reflecting the ongoing dialogue:
|
165 |
{history}
|
166 |
Provide:
|
167 |
-
- A reply to the user's most recent input
|
|
|
168 |
Notes:
|
169 |
-
1-
|
170 |
-
2-
|
171 |
-
3-
|
172 |
-
4-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
"""
|
174 |
|
|
|
175 |
def get_prompt(image_path: str, iter: int, memory: str, firstname: str) -> dict:
|
176 |
|
177 |
if iter == 1:
|
@@ -187,11 +233,18 @@ def get_prompt(image_path: str, iter: int, memory: str, firstname: str) -> dict:
|
|
187 |
parser = question_parser
|
188 |
prompt= CONVERSATION_ENDING_PROMPT.format(history=memory, character=AI_CHARACTER)
|
189 |
|
190 |
-
vision_chain = load_image_chain | image_model |
|
191 |
return vision_chain.invoke({'image_path': f'{image_path}', 'prompt': prompt, 'parser':parser})
|
192 |
|
193 |
|
194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
def retrieve_memory(input_filepath, name):
|
196 |
with open(input_filepath, 'r') as f:
|
197 |
conversation = f.read()
|
@@ -235,111 +288,172 @@ def increment_counts(count_file_path):
|
|
235 |
save_counts(count_file_path, counts)
|
236 |
return counts["count"]
|
237 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
|
239 |
-
|
240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
|
242 |
if user_name.strip() == "":
|
243 |
-
|
244 |
-
return message, message, transform_text_to_speech(message, user_name)
|
245 |
-
|
246 |
-
if image_path:
|
247 |
-
user_name = user_name.strip()
|
248 |
-
image_name = image_path.split("/")[-1]
|
249 |
-
new_image_name = f"{user_name}-{image_name}"
|
250 |
-
new_image_path = f"/data/{new_image_name}"
|
251 |
-
input_filename = f"{user_name}-{image_name}-conversation-memory.txt"
|
252 |
-
input_filepath = f"/data/{input_filename}"
|
253 |
-
count_file_path = f"/data/{user_name}-{image_name}-tracking.json"
|
254 |
-
|
255 |
-
if not os.path.exists(new_image_path):
|
256 |
-
shutil.copy(image_path, new_image_path)
|
257 |
-
iter = increment_counts(count_file_path)
|
258 |
-
output = get_prompt(new_image_path, iter, None, user_name)
|
259 |
-
res = output["question"]
|
260 |
-
with open(input_filepath, 'w') as f:
|
261 |
-
f.write(AI_CHARACTER + ": " + res)
|
262 |
-
return "New Photo Uploaded" , res, transform_text_to_speech(res, user_name)
|
263 |
|
264 |
-
|
|
|
|
|
265 |
|
266 |
-
if audio is not None:
|
267 |
-
user_input = transform_speech_to_text(audio, user_name)
|
268 |
|
269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
with open(input_filepath, 'a') as f:
|
271 |
-
f.write("\n" + user_name + ": " +
|
272 |
with open(input_filepath, 'r') as f:
|
273 |
content = f.read()
|
|
|
274 |
output = get_prompt(new_image_path, iter, content, user_name)
|
275 |
res = output["question"]
|
276 |
with open(input_filepath, 'a') as f:
|
277 |
-
f.write("\n" + AI_CHARACTER + ": "+ res)
|
278 |
-
return
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
# decide the path from the contents of the conversation memory.
|
283 |
-
if os.path.exists(input_filepath):
|
284 |
-
res = retrieve_memory(input_filepath, user_name)
|
285 |
-
if res["speaker"] == AI_CHARACTER:
|
286 |
-
if audio is None:
|
287 |
-
message = ""
|
288 |
-
return "Loading response, please wait...", "Loading response, please wait...", None
|
289 |
-
prefix = "Continuing from where we left off: "
|
290 |
-
return "" , prefix+ res["reply"], transform_text_to_speech(prefix+res["reply"], user_name)
|
291 |
-
else:
|
292 |
-
with open(input_filepath, 'a') as f:
|
293 |
-
f.write("\n" + user_name + ": " + "I'd like to continue our conversation about this photograph.")
|
294 |
-
with open(input_filepath, 'r') as f:
|
295 |
-
content = f.read()
|
296 |
-
iter = increment_counts(count_file_path)
|
297 |
-
output = get_prompt(new_image_path, iter, content, user_name)
|
298 |
-
res = output["question"]
|
299 |
-
with open(input_filepath, 'a') as f:
|
300 |
-
f.write("\n" + AI_CHARACTER + ": " + res)
|
301 |
-
return "I'd like to continue our conversation about this photograph.", res, transform_text_to_speech(res, user_name)
|
302 |
-
|
303 |
-
message = "Great! Please upload a photo to tell your story."
|
304 |
-
return "", message, None
|
305 |
|
306 |
# Backend function to clear inputs
|
307 |
def clear_inputs(user_name, image_path):
|
308 |
-
message = "Great! Please upload a photo to
|
309 |
-
|
310 |
-
return None, None, "", message, transform_text_to_speech(message, user_name)
|
311 |
-
|
312 |
-
image_name = image_path.split("/")[-1]
|
313 |
-
input_filename = f"{user_name}-{image_name}-conversation-memory.txt"
|
314 |
-
input_filepath = f"/data/{input_filename}"
|
315 |
-
if os.path.exists(input_filepath):
|
316 |
-
with open(input_filepath, 'a') as f:
|
317 |
-
f.write("\n" + f"{user_name}: " + "new photo uploaded")
|
318 |
|
319 |
-
message = "Great!"
|
320 |
-
return None, None, "", "", None
|
321 |
|
322 |
# Gradio Interface
|
323 |
with gr.Blocks(title = "KitchenTable.AI") as demo:
|
324 |
with gr.Row():
|
325 |
with gr.Column():
|
326 |
-
clear_button = gr.Button("Tell a
|
327 |
username = gr.Textbox(label="Enter your first name")
|
328 |
image_input = gr.Image(type="filepath", label="Upload an Image")
|
329 |
-
audio_input = gr.Audio(sources="microphone", type="numpy",
|
330 |
-
|
331 |
|
332 |
with gr.Column():
|
333 |
# Output fields
|
334 |
-
|
335 |
-
stud_output = gr.Textbox(label="Studs Terkel")
|
336 |
audio_output = gr.HTML(label="Audio Player")
|
337 |
|
338 |
-
|
339 |
-
|
|
|
|
|
340 |
|
341 |
-
|
342 |
-
clear_button.click(fn=clear_inputs, inputs=[username, image_input], outputs=[image_input, audio_input, user_input_output, stud_output, audio_output])
|
343 |
|
344 |
# Launch the interface
|
345 |
demo.launch(share=True, debug=True)
|
|
|
|
|
1 |
import base64
|
2 |
import logging
|
3 |
import numpy as np
|
|
|
20 |
from langchain_core.runnables import chain
|
21 |
from langchain_core.output_parsers import JsonOutputParser
|
22 |
from langchain.memory import ConversationSummaryBufferMemory, ConversationBufferMemory
|
23 |
+
from datetime import datetime
|
24 |
|
25 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
26 |
client = OpenAI()
|
27 |
|
28 |
def transform_text_to_speech(text: str, user):
|
|
|
97 |
"""Information about an image."""
|
98 |
question: str = Field(description= "A question to start converstion around the photograph")
|
99 |
|
100 |
+
class UserIntent(BaseModel):
|
101 |
+
intent: str = Field(description= "Intent of the user")
|
102 |
+
|
103 |
question_parser = JsonOutputParser(pydantic_object=GenerateQuestion)
|
104 |
starting_question_parser = JsonOutputParser(pydantic_object=StartingQuestion)
|
105 |
+
intent_parser = JsonOutputParser(pydantic_object=UserIntent)
|
106 |
|
107 |
+
@chain
|
108 |
+
def input_model(inputs: dict) -> str | list[str] | dict:
|
109 |
+
"""Invoke model with image and prompt."""
|
110 |
+
model = ChatOpenAI(temperature=0.5, model="gpt-4o", max_tokens=1024)
|
111 |
+
msg = model.invoke(
|
112 |
+
[HumanMessage(
|
113 |
+
content=[
|
114 |
+
{"type": "text", "text": inputs["prompt"]},
|
115 |
+
{"type": "text", "text": inputs["parser"].get_format_instructions()},
|
116 |
+
])]
|
117 |
+
)
|
118 |
+
return msg.content
|
119 |
|
120 |
@chain
|
121 |
def image_model(inputs: dict) -> str | list[str] | dict:
|
|
|
132 |
return msg.content
|
133 |
|
134 |
|
135 |
+
|
136 |
+
|
137 |
+
AI_CHARACTER = "Good Friend"
|
138 |
|
139 |
CONVERSATION_STARTER_PROMPT = """
|
140 |
You are playing the role of a {character} who is interested in learning about the user by asking them questions about the photo they’ve uploaded.
|
141 |
Provide:
|
142 |
- A question to start the conversation around the photograph.
|
143 |
Note:
|
144 |
+
1. You first want to know about the photo. Focus on the people in the photo and the relationships. For example, ask who is in it, where it was taken, or if it was a special occasion.
|
145 |
+
2. Avoid questions about emotions or feelings. Keep questions simple and open ended.
|
146 |
+
3. Don't ask about things in the photo.
|
147 |
+
4. Ask them if there are topics they would like to talk about.
|
148 |
+
|
149 |
"""
|
150 |
|
151 |
CONVERSATION_STARTER2_PROMPT = """
|
|
|
154 |
{history}
|
155 |
Provide:
|
156 |
- A question about the contents of the photograph.
|
157 |
+
|
158 |
Note:
|
159 |
1. You first want to know about the contents of the photo. For example, ask who is in it, where it was taken, or if it was a special occasion.
|
160 |
+
2. Do not repeat questions. Don't ask a very personal question.
|
161 |
+
3. Focus on people and relationships. Keep questions simple and open ended.
|
162 |
+
4. Use conversation history.
|
163 |
+
5. Ask about people mentioned in user's responses not just in photo.
|
164 |
+
6. Don't ask about things in the photo unless the person brings them up
|
165 |
+
7. Avoid platitudes
|
166 |
+
|
167 |
"""
|
168 |
|
169 |
+
|
170 |
CONVERSATION_EXPANDING_PROMPT = """
|
171 |
+
You are playing the role of a good friend who is interested in learning about the user by asking them questions about the photo they’ve uploaded.
|
172 |
You are currently in the middle of a conversation with the user.
|
173 |
+
Here is the conversation history about the image between the user and you (the good friend), reflecting the ongoing dialogue:
|
174 |
{history}
|
175 |
Provide:
|
176 |
+
- A reply to the user's most recent input and a follow-up question that encourages them to expand on their answer about the people in the photograph or mentioned in their response
|
177 |
Notes:
|
178 |
+
1- Ask them about any stories they are reminded of and please ask only one question.
|
179 |
+
2- Do not repeat questions or ask about information that has already been covered.
|
180 |
+
3- Encourage full responses by asking open-ended questions that invite further elaboration.
|
|
|
181 |
5- Use the conversation history to inform your question, while maintaining the flow of the ongoing conversation.
|
182 |
+
6- Don't ask about things in the photo unless the person brings them up
|
183 |
+
7- Avoid platitudes.
|
184 |
+
|
185 |
"""
|
186 |
|
187 |
|
|
|
190 |
Here is the conversation history about the image between the user and you ({character}): reflecting the ongoing dialogue:
|
191 |
{history}
|
192 |
Provide:
|
193 |
+
- A reply to the user's most recent input and a follow-up question that encourages them to share more about the story depicted in the photograph,
|
194 |
+
discuss anything that the photograph reminds them of, or move on to another photograph or stop reminiscing.
|
195 |
Notes:
|
196 |
+
1- Ask them if they want to keep talking about the photo or move onto another photo.
|
197 |
+
2- please ask only one question.
|
198 |
+
3- Ask them to summarize how they feel about the photo
|
199 |
+
4- Do not repeat questions or ask about information already covered in the conversation.
|
200 |
+
5- Encourage full responses by asking open-ended questions that invite further elaboration.
|
201 |
+
"""
|
202 |
+
|
203 |
+
user_intent_prompt = """
|
204 |
+
The system and user are engaged in a conversation about a photo uploaded by the user.
|
205 |
+
The system asks questions related to the photograph, and the user responds.
|
206 |
+
Your task is to analyze the user's input to accurately determine their intent.
|
207 |
+
|
208 |
+
The possible intents are:
|
209 |
+
1. "change photo" - The user explicitly or implicitly indicates they want to move on to the next photo, do not wish to discuss the current photo, or directly state a desire to stop talking about the current photograph.
|
210 |
+
2. "change topic" - The user expresses a desire to talk about something else within the context of the current photograph or shows disinterest in the current line of questioning but doesn't want to change the photograph itself.
|
211 |
+
3. "continue" - The user is comfortable with the current conversation and wants to continue discussing the current photo.
|
212 |
+
|
213 |
+
Here is the user's input:
|
214 |
+
{input}
|
215 |
+
|
216 |
+
Provide:
|
217 |
+
1. The intent of the user.
|
218 |
"""
|
219 |
|
220 |
+
|
221 |
def get_prompt(image_path: str, iter: int, memory: str, firstname: str) -> dict:
|
222 |
|
223 |
if iter == 1:
|
|
|
233 |
parser = question_parser
|
234 |
prompt= CONVERSATION_ENDING_PROMPT.format(history=memory, character=AI_CHARACTER)
|
235 |
|
236 |
+
vision_chain = load_image_chain | image_model | parser
|
237 |
return vision_chain.invoke({'image_path': f'{image_path}', 'prompt': prompt, 'parser':parser})
|
238 |
|
239 |
|
240 |
|
241 |
+
def get_intent(user_input) -> dict:
|
242 |
+
parser = intent_parser
|
243 |
+
prompt = user_intent_prompt.format(input=user_input)
|
244 |
+
intent_chain = input_model | parser
|
245 |
+
return intent_chain.invoke({'prompt': prompt, 'parser':parser})
|
246 |
+
|
247 |
+
|
248 |
def retrieve_memory(input_filepath, name):
|
249 |
with open(input_filepath, 'r') as f:
|
250 |
conversation = f.read()
|
|
|
288 |
save_counts(count_file_path, counts)
|
289 |
return counts["count"]
|
290 |
|
291 |
+
def handle_duration(duration_file_path: str):
|
292 |
+
data = {
|
293 |
+
"starttime": datetime.now().isoformat(),
|
294 |
+
"endtime": None, # Assuming endtime is None initially
|
295 |
+
"duration": 0
|
296 |
+
}
|
297 |
+
with open(duration_file_path, 'w') as json_file:
|
298 |
+
json.dump(data, json_file, indent=4)
|
299 |
+
|
300 |
+
def calculate_duration(duration_file_path: str):
|
301 |
+
with open(duration_file_path, 'r') as json_file:
|
302 |
+
data = json.load(json_file)
|
303 |
+
data['endtime'] = datetime.now().isoformat()
|
304 |
+
starttime = datetime.fromisoformat(data['starttime'])
|
305 |
+
endtime = datetime.fromisoformat(data['endtime'])
|
306 |
+
data['duration'] = (endtime - starttime).total_seconds() / 60 # Convert seconds to minutes
|
307 |
+
with open(duration_file_path, 'w') as json_file:
|
308 |
+
json.dump(data, json_file, indent=4)
|
309 |
+
|
310 |
+
def image_handler(user_name, image_path):
|
311 |
|
312 |
+
if user_name.strip() == "":
|
313 |
+
user_name = "Unknown"
|
314 |
+
|
315 |
+
if not image_path:
|
316 |
+
message = "Great! Please enter your name and upload a photo to share your story."
|
317 |
+
return None, message, transform_text_to_speech(message, user_name)
|
318 |
+
|
319 |
+
|
320 |
+
current_date = datetime.now().strftime("%Y-%m-%d")
|
321 |
+
base_dir = f"/data/{current_date}"
|
322 |
+
os.makedirs(base_dir, exist_ok=True)
|
323 |
+
user_name = user_name.strip()
|
324 |
+
image_name = image_path.split("/")[-1]
|
325 |
+
new_image_name = f"{user_name}-{image_name}"
|
326 |
+
new_image_path = os.path.join(base_dir, new_image_name)
|
327 |
+
input_filename = f"{new_image_name}-conversation-memory.txt"
|
328 |
+
input_filepath = os.path.join(base_dir, input_filename)
|
329 |
+
count_file_path = os.path.join(base_dir, f"{new_image_name}-tracking.json")
|
330 |
+
duration_file_path = os.path.join(base_dir, f"{new_image_name}-duration.json")
|
331 |
+
|
332 |
+
if not os.path.exists(new_image_path):
|
333 |
+
shutil.copy(image_path, new_image_path)
|
334 |
+
iter = increment_counts(count_file_path)
|
335 |
+
handle_duration(duration_file_path)
|
336 |
+
output = get_prompt(new_image_path, iter, None, user_name)
|
337 |
+
res = output["question"]
|
338 |
+
with open(input_filepath, 'w') as f:
|
339 |
+
f.write(AI_CHARACTER + ": " + res)
|
340 |
+
return res, transform_text_to_speech(res, user_name)
|
341 |
+
|
342 |
+
else:
|
343 |
+
res = retrieve_memory(input_filepath, user_name)
|
344 |
+
if res["speaker"] == AI_CHARACTER:
|
345 |
+
qa = "Continuing from where we left off— "+ res["reply"]
|
346 |
+
return qa, transform_text_to_speech(qa , user_name)
|
347 |
+
else:
|
348 |
+
with open(input_filepath, 'a') as f:
|
349 |
+
f.write("\n" + user_name + ": " + "I'd like to continue our conversation about this photograph.")
|
350 |
+
with open(input_filepath, 'r') as f:
|
351 |
+
content = f.read()
|
352 |
+
iter = increment_counts(count_file_path)
|
353 |
+
output = get_prompt(new_image_path, iter, content, user_name)
|
354 |
+
res = output["question"]
|
355 |
+
with open(input_filepath, 'a') as f:
|
356 |
+
f.write("\n" + AI_CHARACTER + ": " + res)
|
357 |
+
return res, transform_text_to_speech(res, user_name)
|
358 |
+
|
359 |
+
|
360 |
+
|
361 |
+
|
362 |
+
def audio_handler(user_name, image_path, audio):
|
363 |
|
364 |
if user_name.strip() == "":
|
365 |
+
user_name = "Unknown"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
366 |
|
367 |
+
if not image_path:
|
368 |
+
message = "Great! Please enter your name and upload a photo to share your story."
|
369 |
+
return None, message, transform_text_to_speech(message, user_name)
|
370 |
|
|
|
|
|
371 |
|
372 |
+
current_date = datetime.now().strftime("%Y-%m-%d")
|
373 |
+
base_dir = f"/data/{current_date}"
|
374 |
+
os.makedirs(base_dir, exist_ok=True)
|
375 |
+
user_name = user_name.strip()
|
376 |
+
image_name = image_path.split("/")[-1]
|
377 |
+
new_image_name = f"{user_name}-{image_name}"
|
378 |
+
new_image_path = os.path.join(base_dir, new_image_name)
|
379 |
+
input_filename = f"{new_image_name}-conversation-memory.txt"
|
380 |
+
input_filepath = os.path.join(base_dir, input_filename)
|
381 |
+
count_file_path = os.path.join(base_dir, f"{new_image_name}-tracking.json")
|
382 |
+
duration_file_path = os.path.join(base_dir, f"{new_image_name}-duration.json")
|
383 |
+
|
384 |
+
|
385 |
+
|
386 |
+
if audio is not None:
|
387 |
+
user_input = transform_speech_to_text(audio, user_name)
|
388 |
+
|
389 |
+
iter = increment_counts(count_file_path)
|
390 |
+
with open(input_filepath, 'a') as f:
|
391 |
+
f.write("\n" + user_name + ": " + user_input)
|
392 |
+
|
393 |
+
output = get_intent(user_input)
|
394 |
+
|
395 |
+
if(output["intent"].strip() == "change photo"):
|
396 |
+
res = "Press the 'Tell a New Story' button to move to a new photograph."
|
397 |
+
else:
|
398 |
+
with open(input_filepath, 'r') as f:
|
399 |
+
content = f.read()
|
400 |
+
output = get_prompt(new_image_path, iter, content, user_name)
|
401 |
+
res = output["question"]
|
402 |
+
with open(input_filepath, 'a') as f:
|
403 |
+
f.write("\n" + AI_CHARACTER + ": "+ res)
|
404 |
+
calculate_duration(duration_file_path)
|
405 |
+
return None, None, None
|
406 |
+
|
407 |
+
|
408 |
+
|
409 |
+
# decide the path from the contents of the conversation memory.
|
410 |
+
if os.path.exists(input_filepath):
|
411 |
+
res = retrieve_memory(input_filepath, user_name)
|
412 |
+
if res["speaker"] == AI_CHARACTER:
|
413 |
+
return None, res["reply"], transform_text_to_speech(res["reply"], user_name)
|
414 |
+
|
415 |
+
else:
|
416 |
with open(input_filepath, 'a') as f:
|
417 |
+
f.write("\n" + user_name + ": " + "I'd like to continue our conversation about this photograph.")
|
418 |
with open(input_filepath, 'r') as f:
|
419 |
content = f.read()
|
420 |
+
iter = increment_counts(count_file_path)
|
421 |
output = get_prompt(new_image_path, iter, content, user_name)
|
422 |
res = output["question"]
|
423 |
with open(input_filepath, 'a') as f:
|
424 |
+
f.write("\n" + AI_CHARACTER + ": " + res)
|
425 |
+
return None, res, transform_text_to_speech(res, user_name)
|
426 |
+
|
427 |
+
|
428 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
429 |
|
430 |
# Backend function to clear inputs
|
431 |
def clear_inputs(user_name, image_path):
|
432 |
+
message = "Great! Please enter your name and upload a photo to share your story."
|
433 |
+
return None, None, message, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
434 |
|
|
|
|
|
435 |
|
436 |
# Gradio Interface
|
437 |
with gr.Blocks(title = "KitchenTable.AI") as demo:
|
438 |
with gr.Row():
|
439 |
with gr.Column():
|
440 |
+
clear_button = gr.Button("Tell a New Story", elem_id="clear-button")
|
441 |
username = gr.Textbox(label="Enter your first name")
|
442 |
image_input = gr.Image(type="filepath", label="Upload an Image")
|
443 |
+
audio_input = gr.Audio(sources="microphone", type="numpy", value=None)
|
444 |
+
|
445 |
|
446 |
with gr.Column():
|
447 |
# Output fields
|
448 |
+
stud_output = gr.Textbox(label="Good Friend")
|
|
|
449 |
audio_output = gr.HTML(label="Audio Player")
|
450 |
|
451 |
+
image_input.change(image_handler, inputs=[username, image_input], outputs=[stud_output, audio_output])
|
452 |
+
audio_input.change(audio_handler, inputs=[username, image_input, audio_input], outputs=[audio_input, stud_output, audio_output])
|
453 |
+
|
454 |
+
|
455 |
|
456 |
+
clear_button.click(fn=clear_inputs, inputs=[username, image_input], outputs=[image_input, audio_input, stud_output, audio_output])
|
|
|
457 |
|
458 |
# Launch the interface
|
459 |
demo.launch(share=True, debug=True)
|