Use other model options for translation. Show city image

This commit is contained in:
Elijah Rwothoromo
2025-08-11 09:50:49 +03:00
parent efcd9af9af
commit f4e9798f3d

View File

@@ -19,7 +19,7 @@
"source": [ "source": [
"# imports\n", "# imports\n",
"\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 dotenv import load_dotenv\n",
"from openai import OpenAI" "from openai import OpenAI"
] ]
@@ -42,7 +42,22 @@
" print(\"OpenAI API Key not set\")\n", " print(\"OpenAI API Key not set\")\n",
" \n", " \n",
"MODEL = \"gpt-4o-mini\"\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(message)\n",
" messages.append(response_tool)\n", " messages.append(response_tool)\n",
" image = artist(city)\n", " image = artist(city)\n",
" print(\"Avail image for: \", city)\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n", " response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
"\n", "\n",
"\n", "\n",
@@ -167,7 +183,8 @@
"source": [ "source": [
"# And this is included in a list of tools:\n", "# And this is included in a list of tools:\n",
"\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", " 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", " \n",
" try:\n", " try:\n",
" response = openai.chat.completions.create(\n", " # # Using OpenAI\n",
" model=MODEL,\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", " messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt_for_language},\n", " {\"role\": \"user\", \"content\": text},\n",
" {\"role\": \"user\", \"content\": text}\n",
" ],\n", " ],\n",
" )\n", " )\n",
" result = response.choices[0].message.content\n", " result = response.content[0].text\n",
" return result\n", " return result\n",
" \n", " \n",
" except Exception as e:\n", " except Exception as e:\n",
@@ -443,7 +485,7 @@
"source": [ "source": [
"def artist(city):\n", "def artist(city):\n",
" image_response = openai.images.generate(\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", " 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", " size=\"1024x1024\",\n",
" n=1,\n", " n=1,\n",
@@ -467,7 +509,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "f4975b87-19e9-4ade-a232-9b809ec75c9a", "id": "6dd849b5-31ae-4237-9072-46b210792bf9",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Audio (NOTE - Audio is optional for this course - feel free to skip Audio if it causes trouble!)\n", "## 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", "def talker(message):\n",
" response = openai.audio.speech.create(\n", " response = openai.audio.speech.create(\n",
" model=\"tts-1\",\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", " input=message\n",
" )\n", " )\n",
" \n", " \n",
@@ -575,7 +617,9 @@
" chatbot = gr.Chatbot(height=500)\n", " chatbot = gr.Chatbot(height=500)\n",
" image = gr.Image(height=500)\n", " image = gr.Image(height=500)\n",
" with gr.Row():\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", " with gr.Row():\n",
" languages = [\"English\", \"Swahili\", \"French\", \"Chinese\", \"German\"]\n", " languages = [\"English\", \"Swahili\", \"French\", \"Chinese\", \"German\"]\n",
" language_dropdown = gr.Dropdown(\n", " language_dropdown = gr.Dropdown(\n",
@@ -597,10 +641,9 @@
" def user_message_updater(user_message, history):\n", " def user_message_updater(user_message, history):\n",
" return \"\", history + [[user_message, None]]\n", " return \"\", history + [[user_message, None]]\n",
"\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", " message = history[-1][0] # Get the user's message from the last list in history\n",
" \n", " \n",
" # Use the existing chat logic\n",
" messages = [{\"role\": \"system\", \"content\": system_message}]\n", " messages = [{\"role\": \"system\", \"content\": system_message}]\n",
" for msg_user, msg_assistant in history:\n", " for msg_user, msg_assistant in history:\n",
" messages.append({\"role\": \"user\", \"content\": msg_user})\n", " messages.append({\"role\": \"user\", \"content\": msg_user})\n",
@@ -608,11 +651,17 @@
" messages.append({\"role\": \"assistant\", \"content\": msg_assistant})\n", " messages.append({\"role\": \"assistant\", \"content\": msg_assistant})\n",
" \n", " \n",
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\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", " if response.choices[0].finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n", " message = response.choices[0].message\n",
" response_tool, city = handle_tool_call(message)\n", " response_tool, city = handle_tool_call(message)\n",
"\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(message.model_dump()) # Append message as a dictionary using .model_dump()\n",
" messages.append(response_tool)\n", " messages.append(response_tool)\n",
" \n", " \n",
@@ -628,11 +677,12 @@
"\n", "\n",
" history[-1][1] = final_response_content\n", " history[-1][1] = final_response_content\n",
"\n", "\n",
" if audio_response != \"No\":\n", " if use_audio != \"No\":\n",
" talker(final_response_content)\n", " talker(final_response_content)\n",
"\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", "\n",
" # The event listeners are updated to be triggered by both the textbox and the new button\n",
" entry.submit(\n", " entry.submit(\n",
" user_message_updater,\n", " user_message_updater,\n",
" inputs=[entry, chatbot],\n", " inputs=[entry, chatbot],\n",
@@ -643,28 +693,23 @@
" inputs=[chatbot, language_dropdown, audio_dropdown],\n", " inputs=[chatbot, language_dropdown, audio_dropdown],\n",
" outputs=[chatbot, image]\n", " outputs=[chatbot, image]\n",
" )\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", " \n",
" clear.click(lambda: None, inputs=None, outputs=[chatbot, image], queue=False)\n", " clear.click(lambda: None, inputs=None, outputs=[chatbot, image], queue=False)\n",
"\n", "\n",
"ui.launch(inbrowser=True)" "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", "cell_type": "code",
"execution_count": null, "execution_count": null,