diff --git a/week2/community-contributions/pptx_summarizer/Leonardo_da_Vinci.pptx b/week2/community-contributions/pptx_summarizer/Leonardo_da_Vinci.pptx new file mode 100644 index 0000000..0d19192 Binary files /dev/null and b/week2/community-contributions/pptx_summarizer/Leonardo_da_Vinci.pptx differ diff --git a/week2/community-contributions/pptx_summarizer/Malala_Yousafzai.pptx b/week2/community-contributions/pptx_summarizer/Malala_Yousafzai.pptx new file mode 100644 index 0000000..2ae09e8 Binary files /dev/null and b/week2/community-contributions/pptx_summarizer/Malala_Yousafzai.pptx differ diff --git a/week2/community-contributions/pptx_summarizer/Marie_Curie.pptx b/week2/community-contributions/pptx_summarizer/Marie_Curie.pptx new file mode 100644 index 0000000..7f2b260 Binary files /dev/null and b/week2/community-contributions/pptx_summarizer/Marie_Curie.pptx differ diff --git a/week2/community-contributions/pptx_summarizer/Nelson_Mandela.pptx b/week2/community-contributions/pptx_summarizer/Nelson_Mandela.pptx new file mode 100644 index 0000000..f5ad864 Binary files /dev/null and b/week2/community-contributions/pptx_summarizer/Nelson_Mandela.pptx differ diff --git a/week2/community-contributions/pptx_summarizer/Shakespeare_Highlights.pptx b/week2/community-contributions/pptx_summarizer/Shakespeare_Highlights.pptx new file mode 100644 index 0000000..29c7fdb Binary files /dev/null and b/week2/community-contributions/pptx_summarizer/Shakespeare_Highlights.pptx differ diff --git a/week2/community-contributions/pptx_summarizer/pptx summarizer.ipynb b/week2/community-contributions/pptx_summarizer/pptx summarizer.ipynb new file mode 100644 index 0000000..476cb76 --- /dev/null +++ b/week2/community-contributions/pptx_summarizer/pptx summarizer.ipynb @@ -0,0 +1,378 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "ddfa9ae6-69fe-444a-b994-8c4c5970a7ec", + "metadata": {}, + "source": [ + "# Project - PPTX summarizer AI Assistant\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "8b50bbe2-c0b1-49c3-9a5c-1ba7efa2bcb4", + "metadata": {}, + "outputs": [], + "source": [ + "# imports\n", + "\n", + "import os\n", + "import json\n", + "from dotenv import load_dotenv\n", + "from openai import OpenAI\n", + "import gradio as gr\n", + "from IPython.display import Markdown, display, update_display" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "747e8786-9da8-4342-b6c9-f5f69c2e22ae", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "OpenAI API Key exists and begins sk-proj-\n" + ] + } + ], + "source": [ + "# Initialization\n", + "\n", + "load_dotenv(override=True)\n", + "\n", + "openai_api_key = os.getenv('OPENAI_API_KEY')\n", + "if openai_api_key:\n", + " print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n", + "else:\n", + " print(\"OpenAI API Key not set\")\n", + " \n", + "MODEL = \"gpt-4o-mini\"\n", + "openai = OpenAI()\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "bac7ac31-6ece-4aa1-87eb-329b82f0f45a", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "import os\n", + "from pptx import Presentation\n", + "import openai # adapt if you use a different client\n", + "\n", + "class PptxSummary:\n", + " def __init__(self, name: str):\n", + " self.name = name\n", + "\n", + " def summary(self):\n", + " # Load an existing PowerPoint\n", + " prs = Presentation(self.name)\n", + " newtext = \"\"\n", + "\n", + " # print(prs)\n", + " # Loop over all slides\n", + " for i, slide in enumerate(prs.slides, start=1):\n", + " # print(f\"\\n--- Slide {i} ---\")\n", + " newtext += f\"\\n\\n--- Slide {i} ---\"\n", + " \n", + " # Loop over shapes (text boxes, titles, placeholders, etc.)\n", + " for shape in slide.shapes:\n", + " if shape.has_text_frame: # Only shapes that can contain text\n", + " for paragraph in shape.text_frame.paragraphs:\n", + " # Collect text from each run in the paragraph\n", + " text = \"\".join(run.text for run in paragraph.runs)\n", + " # print(text)\n", + " newtext+= \"\\n\"\n", + " newtext += text\n", + " # print(newtext)\n", + " return newtext\n", + "\n", + " \n", + "system_message = \"You are a helpful assistant for a company and you can summarize the given topic based on the given pptx name. \"\n", + "system_message += \"Give short, courteous answers, no more than 10 sentence. \"\n", + "system_message += \"Always be accurate. If the presentation .pptx file does not exist, say so. Respond in markdown.\"\n", + "\n", + "def user_message(path):\n", + " ppt = PptxSummary(path)\n", + " summarization_message = ppt.summary()\n", + " message = \"You need to summarize the a pptx file.\"\n", + " message += f\"The context of that pptx file is here: {summarization_message}\"\n", + " message += \"Give the concise information in small paragraphs. \"\n", + " return message\n", + "\n", + "def pptx_summary(path):\n", + " if os.path.exists(path):\n", + " result = \"The file does not exist\"\n", + " response = openai.chat.completions.create(\n", + " model=MODEL,\n", + " messages=[\n", + " {\"role\": \"system\", \"content\": system_message},\n", + " {\"role\": \"user\", \"content\": user_message(path)}\n", + " ],\n", + " )\n", + " result = response.choices[0].message.content\n", + " return result\n", + " else:\n", + " return \"The file does not exist\"\n", + "\n", + "\n", + " \n", + "\n", + "# print(\"Current working directory:\", os.getcwd())\n", + "# print(\"Files here:\", os.listdir())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "e4747d6b-d224-4ab3-b9bb-81c423497c81", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "# Summary of \"Marie Curie: Highlights of a Notable Life\"\n", + "\n", + "The presentation details the life and achievements of Marie Curie, born in Warsaw, Poland in 1867. She pursued her studies in physics and mathematics in Paris, setting the stage for her groundbreaking scientific career.\n", + "\n", + "Marie Curie made history as the first woman to win a Nobel Prize and is renowned for her discovery of radioactivity, alongside her husband, Pierre Curie. Her noteworthy accomplishments include winning Nobel Prizes in both Physics and Chemistry, highlighting her significant contributions to science.\n", + "\n", + "Curie's legacy extends beyond her scientific achievements; she shattered barriers for women in science and her research ultimately aided in the development of medical X-rays. She is celebrated as one of the most influential scientists of all time, leaving an enduring impact on both science and society.\n" + ] + } + ], + "source": [ + "print(pptx_summary(\"Marie_Curie.pptx\"))" + ] + }, + { + "cell_type": "markdown", + "id": "5bd2b368-960c-40b4-adf0-db40fe713cbc", + "metadata": {}, + "source": [ + "# My tools" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "8f7c0fda-704b-4e3d-bc69-89986cf5e7a9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The file does not exist\n" + ] + } + ], + "source": [ + "print(pptx_summary(\"bts.pptx\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c3f8e54f-af95-4bf6-a202-89e7a3ffdec5", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "36bedabf-a0a7-4985-ad8e-07ed6a55a3a4", + "metadata": {}, + "source": [ + "## Tools\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "4afceded-7178-4c05-8fa6-9f2085e6a344", + "metadata": {}, + "outputs": [], + "source": [ + "# There's a particular dictionary structure that's required to describe our function:\n", + "\n", + "summary_function = {\n", + " \"name\": \"pptx_summary\",\n", + " \"description\": \"Get the summary for the given pptx file, you need to call this function, if user asks for a pptx file, if it is outside of the pptx file, tell that you do not know.\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"path\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"The name of the presentation\",\n", + " },\n", + " },\n", + " \"required\": [\"path\"],\n", + " \"additionalProperties\": False\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "bdca8679-935f-4e7f-97e6-e71a4d4f228c", + "metadata": {}, + "outputs": [], + "source": [ + "# And this is included in a list of tools:\n", + "\n", + "tools = [{\"type\": \"function\", \"function\": summary_function}]" + ] + }, + { + "cell_type": "markdown", + "id": "c3d3554f-b4e3-4ce7-af6f-68faa6dd2340", + "metadata": {}, + "source": [ + "## Getting OpenAI to use our Tool\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "ce9b0744-9c78-408d-b9df-9f6fd9ed78cf", + "metadata": {}, + "outputs": [], + "source": [ + "def chat(message, history):\n", + " messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n", + " response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n", + "\n", + " if response.choices[0].finish_reason==\"tool_calls\":\n", + " message = response.choices[0].message\n", + " print(f\"message: {message}\")\n", + " response, city = handle_tool_call(message)\n", + " print(f\"city: {city}, response: {response}\")\n", + " messages.append(message)\n", + " messages.append(response)\n", + " response = openai.chat.completions.create(model=MODEL, messages=messages)\n", + " print(f\"response: {response}\")\n", + " \n", + " return response.choices[0].message.content" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "b0992986-ea09-4912-a076-8e5603ee631f", + "metadata": {}, + "outputs": [], + "source": [ + "# We have to write that function handle_tool_call:\n", + "\n", + "def handle_tool_call(message):\n", + " tool_call = message.tool_calls[0]\n", + " print(f\"tool_call: {tool_call}\")\n", + " arguments = json.loads(tool_call.function.arguments)\n", + " print(f\"arguments: {arguments}\")\n", + " path = arguments.get('path')\n", + " print(f\"path: {path}\")\n", + " summary = pptx_summary(path)\n", + " # print(f\"price: {price}\")\n", + " response = {\n", + " \"role\": \"tool\",\n", + " \"content\": json.dumps({\"path\": path,\"summary\": summary}),\n", + " \"tool_call_id\": tool_call.id\n", + " }\n", + " print(f\"response: {response}\")\n", + " return response, path" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "f4be8a71-b19e-4c2f-80df-f59ff2661f14", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* Running on local URL: http://127.0.0.1:7860\n", + "* To create a public link, set `share=True` in `launch()`.\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "message: ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageFunctionToolCall(id='call_qgCGcnIRiknlEaT6eGFlPyN4', function=Function(arguments='{\"path\":\"nelson_mandela.pptx\"}', name='pptx_summary'), type='function')])\n", + "tool_call: ChatCompletionMessageFunctionToolCall(id='call_qgCGcnIRiknlEaT6eGFlPyN4', function=Function(arguments='{\"path\":\"nelson_mandela.pptx\"}', name='pptx_summary'), type='function')\n", + "arguments: {'path': 'nelson_mandela.pptx'}\n", + "path: nelson_mandela.pptx\n", + "response: {'role': 'tool', 'content': '{\"path\": \"nelson_mandela.pptx\", \"summary\": \"# Summary of \\\\\"Nelson Mandela: Highlights of a Notable Life\\\\\" \\\\n\\\\nThis presentation covers the life and impact of Nelson Mandela. Born in 1918 in South Africa, Mandela became politically active in the fight against apartheid after studying law. \\\\n\\\\nHe emerged as a prominent leader in the anti-apartheid movement but faced imprisonment for 27 years due to his activism. In 1994, he made history by becoming South Africa\\'s first Black president. \\\\n\\\\nMandela\\'s legacy is profound; he is celebrated as a symbol of peace, reconciliation, and justice. His efforts earned him the Nobel Peace Prize and he continues to inspire human rights movements around the globe.\"}', 'tool_call_id': 'call_qgCGcnIRiknlEaT6eGFlPyN4'}\n", + "city: nelson_mandela.pptx, response: {'role': 'tool', 'content': '{\"path\": \"nelson_mandela.pptx\", \"summary\": \"# Summary of \\\\\"Nelson Mandela: Highlights of a Notable Life\\\\\" \\\\n\\\\nThis presentation covers the life and impact of Nelson Mandela. Born in 1918 in South Africa, Mandela became politically active in the fight against apartheid after studying law. \\\\n\\\\nHe emerged as a prominent leader in the anti-apartheid movement but faced imprisonment for 27 years due to his activism. In 1994, he made history by becoming South Africa\\'s first Black president. \\\\n\\\\nMandela\\'s legacy is profound; he is celebrated as a symbol of peace, reconciliation, and justice. His efforts earned him the Nobel Peace Prize and he continues to inspire human rights movements around the globe.\"}', 'tool_call_id': 'call_qgCGcnIRiknlEaT6eGFlPyN4'}\n", + "response: ChatCompletion(id='chatcmpl-CKna164Iq9BrGm35e19Rqm8K6Yr9N', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='# Summary of \"Nelson Mandela: Highlights of a Notable Life\"\\n\\nThis presentation covers the life and impact of Nelson Mandela. Born in 1918 in South Africa, Mandela became politically active in the fight against apartheid after studying law. \\n\\nHe emerged as a prominent leader in the anti-apartheid movement but faced imprisonment for 27 years due to his activism. In 1994, he made history by becoming South Africa\\'s first Black president. \\n\\nMandela\\'s legacy is profound; he is celebrated as a symbol of peace, reconciliation, and justice. His efforts earned him the Nobel Peace Prize, and he continues to inspire human rights movements around the globe.', refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=None))], created=1759073181, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier='default', system_fingerprint='fp_560af6e559', usage=CompletionUsage(completion_tokens=134, prompt_tokens=269, total_tokens=403, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))\n" + ] + } + ], + "source": [ + "gr.ChatInterface(fn=chat, type=\"messages\").launch()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d551ce21-c66c-4f56-88ad-8212f16437ab", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/week2/community-contributions/week2 EXERCISE.ipynb b/week2/community-contributions/week2 EXERCISE.ipynb new file mode 100644 index 0000000..4240925 --- /dev/null +++ b/week2/community-contributions/week2 EXERCISE.ipynb @@ -0,0 +1,304 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d006b2ea-9dfe-49c7-88a9-a5a0775185fd", + "metadata": {}, + "source": [ + "# Additional End of week Exercise - week 2\n", + "\n", + "Now use everything you've learned from Week 2 to build a full prototype for the technical question/answerer you built in Week 1 Exercise.\n", + "\n", + "This should include a Gradio UI, streaming, use of the system prompt to add expertise, and the ability to switch between models. Bonus points if you can demonstrate use of a tool!\n", + "\n", + "If you feel bold, see if you can add audio input so you can talk to it, and have it respond with audio. ChatGPT or Claude can help you, or email me if you have questions.\n", + "\n", + "I will publish a full solution here soon - unless someone beats me to it...\n", + "\n", + "There are so many commercial applications for this, from a language tutor, to a company onboarding solution, to a companion AI to a course (like this one!) I can't wait to see your results." + ] + }, + { + "cell_type": "markdown", + "id": "b6197b62-57fb-4569-a82d-bd4a15f4471b", + "metadata": {}, + "source": [ + "# Imports" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b538792e-2414-4922-bb51-95c61f6c2128", + "metadata": {}, + "outputs": [], + "source": [ + "# imports\n", + "\n", + "import os\n", + "import requests\n", + "import json\n", + "from typing import List\n", + "from dotenv import load_dotenv\n", + "from IPython.display import Markdown, display, update_display, Audio\n", + "from openai import OpenAI\n", + "import anthropic\n", + "import base64\n", + "from io import BytesIO\n", + "from PIL import Image\n", + "import gradio as gr" + ] + }, + { + "cell_type": "markdown", + "id": "95afec00-4fe5-4cc9-ae0a-37c6ea4934d6", + "metadata": {}, + "source": [ + "# OpenAI API key" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6c23ef55-daad-4ecf-893e-b83e507f2732", + "metadata": {}, + "outputs": [], + "source": [ + "# Load environment variables in a file called .env\n", + "# Print the key prefixes to help with any debugging\n", + "\n", + "load_dotenv(override=True)\n", + "openai_api_key = os.getenv('OPENAI_API_KEY')\n", + "anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n", + "\n", + "\n", + "if openai_api_key:\n", + " print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n", + "else:\n", + " print(\"OpenAI API Key not set\")\n", + " \n", + "if anthropic_api_key:\n", + " print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n", + "else:\n", + " print(\"Anthropic API Key not set\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e260a7ea-fe79-4222-b6d7-7982f55a52d7", + "metadata": {}, + "outputs": [], + "source": [ + "openai = OpenAI()\n", + "\n", + "claude = anthropic.Anthropic()" + ] + }, + { + "cell_type": "markdown", + "id": "270e937e-aef9-4032-b51e-ffa0fec184a1", + "metadata": {}, + "source": [ + "# SYSTEM prompt" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5639ebd6-d197-43c3-a1c4-4308b117e5ef", + "metadata": {}, + "outputs": [], + "source": [ + "# set up environment\n", + "\n", + "def system_prompt(selected_model):\n", + " return f\"\"\"\n", + " You are a tech expert and know every coding language, and can give \n", + " nice, detailed and simple explanations for the given questions.\n", + " Introduce yourself by saying which model you are every time you answer. For example, this is {selected_model}. \n", + " \n", + " \"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1d960d3-db2e-4f7c-ab7f-d5816f3bd994", + "metadata": {}, + "outputs": [], + "source": [ + "def talker(message):\n", + " response = openai.audio.speech.create(\n", + " model=\"tts-1\",\n", + " voice=\"onyx\",\n", + " input=message)\n", + "\n", + " audio_stream = BytesIO(response.content)\n", + " output_filename = \"output_audio.mp3\"\n", + " with open(output_filename, \"wb\") as f:\n", + " f.write(audio_stream.read())\n", + "\n", + " display(Audio(output_filename, autoplay=True))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f39f1bc7-87c2-4b44-8ba7-c320acb33361", + "metadata": {}, + "outputs": [], + "source": [ + "def listener(audio_file):\n", + " with open(audio_file, \"rb\") as audio:\n", + " transcript = openai.audio.transcriptions.create(\n", + " model=\"whisper-1\",\n", + " file=audio\n", + " )\n", + " return transcript.text" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f88078a9-49ac-48b1-9f07-5b3977e88a52", + "metadata": {}, + "outputs": [], + "source": [ + "def chat(cleared_entry, history, selected_model):\n", + " messages = [{\"role\": \"system\", \"content\": system_prompt(selected_model)}] + history\n", + "\n", + " print(messages)\n", + " \n", + " if selected_model == \"GPT-4o-mini\":\n", + " stream = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages, stream=True)\n", + " response = \"\"\n", + " \n", + " for chunk in stream:\n", + " try:\n", + " response += chunk.choices[0].delta.content or ''\n", + " \n", + " updated_history = history + [{\"role\": \"assistant\", \"content\": response}]\n", + " # talker(response)\n", + " yield updated_history, None \n", + " except Exception as e:\n", + " print(f\"Streaming error: {e}\")\n", + " yield \"Sorry, there was an error processing your request.\"\n", + " # talker(response)\n", + " elif selected_model == \"Claude-sonnet-4\":\n", + " claude_messages = [{\"role\": msg[\"role\"], \"content\": msg[\"content\"]} for msg in history]\n", + " print(claude_messages)\n", + " result = claude.messages.stream(\n", + " model=\"claude-sonnet-4-20250514\",\n", + " max_tokens=200,\n", + " temperature=0.7,\n", + " system=system_prompt(selected_model), \n", + " messages=claude_messages, \n", + " )\n", + " \n", + " response = \"\"\n", + " with result as stream:\n", + " for text in stream.text_stream:\n", + " response += text\n", + " \n", + " updated_history = history + [{\"role\": \"assistant\", \"content\": response}]\n", + " \n", + " yield updated_history, None \n", + " # talker(response)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d1863d62-a2e8-454b-9296-710c4e5f9396", + "metadata": {}, + "outputs": [], + "source": [ + "with gr.Blocks() as ui:\n", + "\n", + " gr.Markdown(\"## AI Chat Assistant\")\n", + " gr.Markdown(\"**Select your preferred AI model:**\")\n", + " \n", + " model_dropdown = gr.Dropdown(\n", + " choices=[\"GPT-4o-mini\", \"Claude-sonnet-4\"], \n", + " value=\"GPT-4o-mini\", # default selection\n", + " label=\"Choose Model\"\n", + " )\n", + "\n", + " \n", + " with gr.Row():\n", + " chatbot = gr.Chatbot(height=200, type=\"messages\")\n", + " image_output = gr.Image(height=200)\n", + " with gr.Row():\n", + " entry = gr.Textbox(label=\"Chat with our AI Assistant:\")\n", + " with gr.Row():\n", + " # Audio input for voice messages\n", + " audio_input = gr.Audio(\n", + " sources=[\"microphone\", \"upload\"], \n", + " type=\"filepath\", \n", + " label=\"🎙️ Voice Message\"\n", + " )\n", + " with gr.Row():\n", + " voice_submit = gr.Button(\"Send Voice Message\", variant=\"secondary\")\n", + " clear = gr.Button(\"Clear\")\n", + "\n", + "\n", + " def do_entry(message, history):\n", + " history += [{\"role\":\"user\", \"content\":message}]\n", + " return \"\", history\n", + "\n", + " def process_voice_input(audio_file):\n", + " \"\"\"Convert voice to text and put it in the text box\"\"\"\n", + " if audio_file is not None:\n", + " transcribed_text = listener(audio_file)\n", + " if transcribed_text and not transcribed_text.startswith(\"Error\"):\n", + " return transcribed_text\n", + " return \"\"\n", + "\n", + " entry.submit(do_entry, inputs=[entry, chatbot], outputs=[entry, chatbot]).then(\n", + " chat, inputs=[entry,chatbot, model_dropdown], outputs=[chatbot, image_output]\n", + " )\n", + "\n", + " voice_submit.click(\n", + " process_voice_input,\n", + " inputs=[audio_input],\n", + " outputs=[entry]\n", + " )\n", + " \n", + " clear.click(lambda: None, inputs=None, outputs=chatbot, queue=False)\n", + "\n", + "ui.launch(inbrowser=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "46085f59-8945-4a64-9db1-d10cd44284db", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}