From f4e9798f3dd379cb4bc2cd2a1c99c26e694efb30 Mon Sep 17 00:00:00 2001 From: Elijah Rwothoromo Date: Mon, 11 Aug 2025 09:50:49 +0300 Subject: [PATCH] Use other model options for translation. Show city image --- .../rwothoromo/day5.ipynb | 111 ++++++++++++------ 1 file changed, 78 insertions(+), 33 deletions(-) diff --git a/week2/community-contributions/rwothoromo/day5.ipynb b/week2/community-contributions/rwothoromo/day5.ipynb index f591fa9..edd2a73 100644 --- a/week2/community-contributions/rwothoromo/day5.ipynb +++ b/week2/community-contributions/rwothoromo/day5.ipynb @@ -19,7 +19,7 @@ "source": [ "# imports\n", "\n", - "import os, json, gradio as gr\n", + "import os, json, gradio as gr, anthropic, google.generativeai\n", "from dotenv import load_dotenv\n", "from openai import OpenAI" ] @@ -42,7 +42,22 @@ " print(\"OpenAI API Key not set\")\n", " \n", "MODEL = \"gpt-4o-mini\"\n", - "openai = OpenAI()" + "openai = OpenAI()\n", + "\n", + "# Other LLMs\n", + "DALL_E_MODEL = \"dall-e-3\"\n", + "\n", + "CLAUDE_MODEL = \"claude-sonnet-4-20250514\"\n", + "claude = anthropic.Anthropic()\n", + "\n", + "google_api_key = os.getenv('GOOGLE_API_KEY')\n", + "if google_api_key:\n", + " print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n", + "else:\n", + " print(\"Google API Key not set\")\n", + " \n", + "GEMINI_MODEL= \"gemini-2.5-flash\"\n", + "gemini = google.generativeai.configure()" ] }, { @@ -76,6 +91,7 @@ " messages.append(message)\n", " messages.append(response_tool)\n", " image = artist(city)\n", + " print(\"Avail image for: \", city)\n", " response = openai.chat.completions.create(model=MODEL, messages=messages)\n", "\n", "\n", @@ -167,7 +183,8 @@ "source": [ "# And this is included in a list of tools:\n", "\n", - "tools = [{\"type\": \"function\", \"function\": price_function}]" + "tools = [{\"type\": \"function\", \"function\": price_function}]\n", + "# print(tools)" ] }, { @@ -259,14 +276,39 @@ " system_prompt_for_language = f\"You are a helpful translation assistant. Translate the following text into {target_language}. Only provide the translated text without any additional conversational text.\"\n", " \n", " try:\n", - " response = openai.chat.completions.create(\n", - " model=MODEL,\n", + " # # Using OpenAI\n", + " # response = openai.chat.completions.create(\n", + " # model=MODEL,\n", + " # messages=[\n", + " # {\"role\": \"system\", \"content\": system_prompt_for_language},\n", + " # {\"role\": \"user\", \"content\": text}\n", + " # ],\n", + " # )\n", + " # result = response.choices[0].message.content\n", + " # return result\n", + "\n", + " \n", + " # # Using Gemini\n", + " # gemini = google.generativeai.GenerativeModel(\n", + " # model_name=GEMINI_MODEL,\n", + " # system_instruction=system_prompt_for_language\n", + " # )\n", + " # response = gemini.generate_content(text)\n", + " # result = response.text\n", + " # return result\n", + "\n", + " \n", + " # Using Claude\n", + " response = claude.messages.create(\n", + " model=CLAUDE_MODEL,\n", + " max_tokens=200,\n", + " temperature=0.7,\n", + " system=system_prompt_for_language,\n", " messages=[\n", - " {\"role\": \"system\", \"content\": system_prompt_for_language},\n", - " {\"role\": \"user\", \"content\": text}\n", + " {\"role\": \"user\", \"content\": text},\n", " ],\n", " )\n", - " result = response.choices[0].message.content\n", + " result = response.content[0].text\n", " return result\n", " \n", " except Exception as e:\n", @@ -443,7 +485,7 @@ "source": [ "def artist(city):\n", " image_response = openai.images.generate(\n", - " model=\"dall-e-3\",\n", + " model=DALL_E_MODEL,\n", " prompt=f\"An image representing a vacation in {city}, showing tourist spots and everything unique about {city}, in a vibrant pop-art style\",\n", " size=\"1024x1024\",\n", " n=1,\n", @@ -467,7 +509,7 @@ }, { "cell_type": "markdown", - "id": "f4975b87-19e9-4ade-a232-9b809ec75c9a", + "id": "6dd849b5-31ae-4237-9072-46b210792bf9", "metadata": {}, "source": [ "## Audio (NOTE - Audio is optional for this course - feel free to skip Audio if it causes trouble!)\n", @@ -541,7 +583,7 @@ "def talker(message):\n", " response = openai.audio.speech.create(\n", " model=\"tts-1\",\n", - " voice=\"alloy\", # Also, try replacing onyx with alloy\n", + " voice=\"onyx\", # Also, try replacing onyx with alloy\n", " input=message\n", " )\n", " \n", @@ -575,7 +617,9 @@ " chatbot = gr.Chatbot(height=500)\n", " image = gr.Image(height=500)\n", " with gr.Row():\n", - " entry = gr.Textbox(label=\"Chat with our AI Assistant:\")\n", + " # entry = gr.Textbox(label=\"Chat with our AI Assistant:\")\n", + " entry = gr.Textbox(label=\"Chat with our AI Assistant:\", scale=4)\n", + " submit_btn = gr.Button(\"Submit\", scale=1)\n", " with gr.Row():\n", " languages = [\"English\", \"Swahili\", \"French\", \"Chinese\", \"German\"]\n", " language_dropdown = gr.Dropdown(\n", @@ -597,10 +641,9 @@ " def user_message_updater(user_message, history):\n", " return \"\", history + [[user_message, None]]\n", "\n", - " def chat_with_assistant(history, target_language, audio_response):\n", + " def chat_with_assistant(history, target_language, use_audio):\n", " message = history[-1][0] # Get the user's message from the last list in history\n", " \n", - " # Use the existing chat logic\n", " messages = [{\"role\": \"system\", \"content\": system_message}]\n", " for msg_user, msg_assistant in history:\n", " messages.append({\"role\": \"user\", \"content\": msg_user})\n", @@ -608,11 +651,17 @@ " messages.append({\"role\": \"assistant\", \"content\": msg_assistant})\n", " \n", " response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n", - " \n", + "\n", + " image = None\n", + " \n", " if response.choices[0].finish_reason==\"tool_calls\":\n", " message = response.choices[0].message\n", " response_tool, city = handle_tool_call(message)\n", "\n", + " # Check if a city was returned from the tool call to generate an image\n", + " if city:\n", + " image = artist(city) # Generate an image to represent the target City\n", + "\n", " messages.append(message.model_dump()) # Append message as a dictionary using .model_dump()\n", " messages.append(response_tool)\n", " \n", @@ -628,11 +677,12 @@ "\n", " history[-1][1] = final_response_content\n", "\n", - " if audio_response != \"No\":\n", + " if use_audio != \"No\":\n", " talker(final_response_content)\n", "\n", - " return history, None # Return a tuple of (the updated history, an empty image)\n", + " return history, image # Return a tuple of (the updated history, an image)\n", "\n", + " # The event listeners are updated to be triggered by both the textbox and the new button\n", " entry.submit(\n", " user_message_updater,\n", " inputs=[entry, chatbot],\n", @@ -643,28 +693,23 @@ " inputs=[chatbot, language_dropdown, audio_dropdown],\n", " outputs=[chatbot, image]\n", " )\n", + "\n", + " submit_btn.click(\n", + " user_message_updater,\n", + " inputs=[entry, chatbot],\n", + " outputs=[entry, chatbot],\n", + " queue=False\n", + " ).then(\n", + " chat_with_assistant,\n", + " inputs=[chatbot, language_dropdown, audio_dropdown],\n", + " outputs=[chatbot, image]\n", + " )\n", " \n", " clear.click(lambda: None, inputs=None, outputs=[chatbot, image], queue=False)\n", "\n", "ui.launch(inbrowser=True)" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "26ab4bd7-b7c4-4e1c-aa51-578326660370", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "69a51144-0e51-4bbf-b5d0-a9e0a60d05ae", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "code", "execution_count": null,