Spaces:
Running
on
Zero
Running
on
Zero
Fix key error in tool call
Browse files
app.py
CHANGED
|
@@ -1,95 +1,95 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import shutil
|
| 3 |
-
|
| 4 |
-
import gradio as gr
|
| 5 |
-
from smolagents import ChatMessageToolCall, ActionStep, FinalAnswerStep
|
| 6 |
-
|
| 7 |
-
import utils
|
| 8 |
-
from agent import VideoChatbot
|
| 9 |
-
from configs import settings
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
bot = VideoChatbot(
|
| 13 |
-
model=settings.CHATBOT_MODEL,
|
| 14 |
-
api_base=settings.MODEL_BASE_API,
|
| 15 |
-
api_key=os.environ['GEMINI_API_KEY']
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def chat(message: dict, history: list[dict]):
|
| 20 |
-
|
| 21 |
-
# move the file to the data directory
|
| 22 |
-
message['files'] = [shutil.copy(file, settings.DATA_DIR) for file in message['files']]
|
| 23 |
-
|
| 24 |
-
# add the input message to the history
|
| 25 |
-
history.extend([{'role': 'user', 'content': {'path': file}} for file in message['files']])
|
| 26 |
-
history.append({'role': 'user', 'content': message['text']})
|
| 27 |
-
yield history, ''
|
| 28 |
-
|
| 29 |
-
for step in bot.chat(message['text'], message['files']):
|
| 30 |
-
match step:
|
| 31 |
-
case ChatMessageToolCall():
|
| 32 |
-
if step.function.name == 'download_video':
|
| 33 |
-
history.append({
|
| 34 |
-
'role': 'assistant',
|
| 35 |
-
'content': f'π₯ Downloading video from {step.function.arguments["url"]}'
|
| 36 |
-
})
|
| 37 |
-
elif step.function.name == 'index_video':
|
| 38 |
-
video_path = os.path.join(settings.DATA_DIR, step.function.arguments['filename'])
|
| 39 |
-
video_duration = utils.seconds_to_hms(int(utils.get_media_duration(video_path)))
|
| 40 |
-
history.append({
|
| 41 |
-
'role': 'assistant',
|
| 42 |
-
'content': f'π₯ Indexing video `{step.function.arguments["filename"]}` with length
|
| 43 |
-
f'to the knowledge base. This may take a while...'
|
| 44 |
-
})
|
| 45 |
-
elif step.function.name == 'search_video_segments':
|
| 46 |
-
filename = os.path.basename(bot.video_rag.videos[step.function.arguments["video_id"]]['video_path'])
|
| 47 |
-
history.append({
|
| 48 |
-
'role': 'assistant',
|
| 49 |
-
'content': f'π Searching video segments in `{filename}` '
|
| 50 |
-
f'for query: *{step.function.arguments.get("text_query", step.function.arguments.get("image_query", ""))}*'
|
| 51 |
-
})
|
| 52 |
-
elif step.function.name == 'read_video_segment':
|
| 53 |
-
filename = os.path.basename(bot.video_rag.videos[step.function.arguments["video_id"]]['video_path'])
|
| 54 |
-
history.append({
|
| 55 |
-
'role': 'assistant',
|
| 56 |
-
'content': f'π Reading video segment `{filename}` '
|
| 57 |
-
f'from
|
| 58 |
-
})
|
| 59 |
-
elif step.function.name == 'final_answer':
|
| 60 |
-
continue
|
| 61 |
-
yield history, ''
|
| 62 |
-
case ActionStep():
|
| 63 |
-
yield history, ''
|
| 64 |
-
case FinalAnswerStep():
|
| 65 |
-
history.append({'role': 'assistant', 'content': step.output})
|
| 66 |
-
yield history, ''
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
def clear_chat(chatbot):
|
| 70 |
-
chatbot.clear()
|
| 71 |
-
return chatbot, gr.update(value='')
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
def main():
|
| 75 |
-
with gr.Blocks() as demo:
|
| 76 |
-
gr.Markdown('# Video Chatbot Demo')
|
| 77 |
-
gr.Markdown('This demo showcases a video chatbot that can process and search videos using '
|
| 78 |
-
'RAG (Retrieval-Augmented Generation). You can upload videos/images or link to YouTube videos, '
|
| 79 |
-
'ask questions, and get answers based on the video content.')
|
| 80 |
-
chatbot = gr.Chatbot(type='messages', label='Video Chatbot', height=800, resizable=True)
|
| 81 |
-
textbox = gr.MultimodalTextbox(
|
| 82 |
-
sources=['upload'],
|
| 83 |
-
file_types=['image', '.mp4'],
|
| 84 |
-
show_label=False,
|
| 85 |
-
placeholder='Type a message or upload an image/video...',
|
| 86 |
-
|
| 87 |
-
)
|
| 88 |
-
textbox.submit(chat, [textbox, chatbot], [chatbot, textbox])
|
| 89 |
-
clear = gr.Button('Clear Chat')
|
| 90 |
-
clear.click(clear_chat, [chatbot], [chatbot, textbox])
|
| 91 |
-
|
| 92 |
-
demo.launch(debug=True)
|
| 93 |
-
|
| 94 |
-
if __name__ == '__main__':
|
| 95 |
main()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from smolagents import ChatMessageToolCall, ActionStep, FinalAnswerStep
|
| 6 |
+
|
| 7 |
+
import utils
|
| 8 |
+
from agent import VideoChatbot
|
| 9 |
+
from configs import settings
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
bot = VideoChatbot(
|
| 13 |
+
model=settings.CHATBOT_MODEL,
|
| 14 |
+
api_base=settings.MODEL_BASE_API,
|
| 15 |
+
api_key=os.environ['GEMINI_API_KEY']
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def chat(message: dict, history: list[dict]):
|
| 20 |
+
|
| 21 |
+
# move the file to the data directory
|
| 22 |
+
message['files'] = [shutil.copy(file, settings.DATA_DIR) for file in message['files']]
|
| 23 |
+
|
| 24 |
+
# add the input message to the history
|
| 25 |
+
history.extend([{'role': 'user', 'content': {'path': file}} for file in message['files']])
|
| 26 |
+
history.append({'role': 'user', 'content': message['text']})
|
| 27 |
+
yield history, ''
|
| 28 |
+
|
| 29 |
+
for step in bot.chat(message['text'], message['files']):
|
| 30 |
+
match step:
|
| 31 |
+
case ChatMessageToolCall():
|
| 32 |
+
if step.function.name == 'download_video':
|
| 33 |
+
history.append({
|
| 34 |
+
'role': 'assistant',
|
| 35 |
+
'content': f'π₯ Downloading video from {step.function.arguments["url"]}'
|
| 36 |
+
})
|
| 37 |
+
elif step.function.name == 'index_video':
|
| 38 |
+
video_path = os.path.join(settings.DATA_DIR, step.function.arguments['filename'])
|
| 39 |
+
video_duration = utils.seconds_to_hms(int(utils.get_media_duration(video_path)))
|
| 40 |
+
history.append({
|
| 41 |
+
'role': 'assistant',
|
| 42 |
+
'content': f'π₯ Indexing video `{step.function.arguments["filename"]}` with length `{video_duration}` '
|
| 43 |
+
f'to the knowledge base. This may take a while...'
|
| 44 |
+
})
|
| 45 |
+
elif step.function.name == 'search_video_segments':
|
| 46 |
+
filename = os.path.basename(bot.video_rag.videos[step.function.arguments["video_id"]]['video_path'])
|
| 47 |
+
history.append({
|
| 48 |
+
'role': 'assistant',
|
| 49 |
+
'content': f'π Searching video segments in `{filename}` '
|
| 50 |
+
f'for query: *{step.function.arguments.get("text_query", step.function.arguments.get("image_query", ""))}*'
|
| 51 |
+
})
|
| 52 |
+
elif step.function.name == 'read_video_segment':
|
| 53 |
+
filename = os.path.basename(bot.video_rag.videos[step.function.arguments["video_id"]]['video_path'])
|
| 54 |
+
history.append({
|
| 55 |
+
'role': 'assistant',
|
| 56 |
+
'content': f'π Reading video segment `{filename}` '
|
| 57 |
+
f'from `{step.function.arguments["start"]}` to `{step.function.arguments["end"]}`'
|
| 58 |
+
})
|
| 59 |
+
elif step.function.name == 'final_answer':
|
| 60 |
+
continue
|
| 61 |
+
yield history, ''
|
| 62 |
+
case ActionStep():
|
| 63 |
+
yield history, ''
|
| 64 |
+
case FinalAnswerStep():
|
| 65 |
+
history.append({'role': 'assistant', 'content': step.output})
|
| 66 |
+
yield history, ''
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def clear_chat(chatbot):
|
| 70 |
+
chatbot.clear()
|
| 71 |
+
return chatbot, gr.update(value='')
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def main():
|
| 75 |
+
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown('# Video Chatbot Demo')
|
| 77 |
+
gr.Markdown('This demo showcases a video chatbot that can process and search videos using '
|
| 78 |
+
'RAG (Retrieval-Augmented Generation). You can upload videos/images or link to YouTube videos, '
|
| 79 |
+
'ask questions, and get answers based on the video content.')
|
| 80 |
+
chatbot = gr.Chatbot(type='messages', label='Video Chatbot', height=800, resizable=True)
|
| 81 |
+
textbox = gr.MultimodalTextbox(
|
| 82 |
+
sources=['upload'],
|
| 83 |
+
file_types=['image', '.mp4'],
|
| 84 |
+
show_label=False,
|
| 85 |
+
placeholder='Type a message or upload an image/video...',
|
| 86 |
+
|
| 87 |
+
)
|
| 88 |
+
textbox.submit(chat, [textbox, chatbot], [chatbot, textbox])
|
| 89 |
+
clear = gr.Button('Clear Chat')
|
| 90 |
+
clear.click(clear_chat, [chatbot], [chatbot, textbox])
|
| 91 |
+
|
| 92 |
+
demo.launch(debug=True)
|
| 93 |
+
|
| 94 |
+
if __name__ == '__main__':
|
| 95 |
main()
|