<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thomas Chong</title>
    <description>The latest articles on DEV Community by Thomas Chong (@thomas-chong).</description>
    <link>https://dev.to/thomas-chong</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2752805%2Fec77cedf-a469-4351-a2eb-63617898bbd3.jpg</url>
      <title>DEV Community: Thomas Chong</title>
      <link>https://dev.to/thomas-chong</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thomas-chong"/>
    <language>en</language>
    <item>
      <title>Gemini 2.0 Flash: Unleashing Native Image Generation - A Tech Deep Dive</title>
      <dc:creator>Thomas Chong</dc:creator>
      <pubDate>Sat, 15 Mar 2025 15:30:38 +0000</pubDate>
      <link>https://dev.to/thomas-chong/gemini-20-flash-unleashing-native-image-generation-a-tech-deep-dive-2a6h</link>
      <guid>https://dev.to/thomas-chong/gemini-20-flash-unleashing-native-image-generation-a-tech-deep-dive-2a6h</guid>
      <description>&lt;p&gt;Google's Gemini 2.0 Flash is making waves with its groundbreaking native image generation. This "workhorse" AI now crafts and edits visuals directly from text, marking a leap towards true AI multimodality. In this blog post, we will offer a hands-on guide to using Gemini 2.0 Flash for image generation in AI Studio and via the API, showcasing its transformative potential through compelling use cases.&lt;/p&gt;




&lt;h2&gt;
  
  
  Getting Started: How to Use it?
&lt;/h2&gt;

&lt;p&gt;Eager to start playing with AI-powered visuals? Gemini 2.0 Flash offers two user-friendly ways to explore its native image generation: Google AI Studio for a visual approach, and the Gemini API for programmatic control.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.  Google AI Studio: A Visual Playground
&lt;/h3&gt;

&lt;p&gt;If you prefer a hands-on, visual environment, Google AI Studio is the ideal launchpad. It's an intuitive platform for experimenting with the experimental Gemini 2.0 Flash, often labeled as &lt;code&gt;gemini-2.0-flash-exp-image-generation&lt;/code&gt;, alias that points to &lt;code&gt;gemini-2.0-flash-exp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here's your quick start guide to AI Studio:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 1. &lt;strong&gt;Model Selection:&lt;/strong&gt;  Within AI Studio, pinpoint and choose the &lt;code&gt;Gemini 2.0 Flash (Image Generation) Experimental&lt;/code&gt; model.&lt;/li&gt;
&lt;li&gt;Step 2. &lt;strong&gt;Output Configuration:&lt;/strong&gt;  Set the output format to &lt;strong&gt;"Images + text"&lt;/strong&gt;. This simple but essential step instructs the model to include images alongside any text responses.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ry052uhmfojo25sy6e6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ry052uhmfojo25sy6e6.png" alt="A screenshot of the Google AI Studio interface. On the right sidebar, under " width="223" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Step 3. &lt;strong&gt;Jump into Text-to-Image Prompts:&lt;/strong&gt; Time to get creative!  Try starting with prompts like these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Imagine a cyberpunk cityscape at sunset. Generate a picture of it.&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Create a vivid image of the Eiffel Tower exploding with fireworks during Bastille Day.&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;These initial prompts will give you a feel for the visual requests Gemini 2.0 Flash can handle and inspire more complex creations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fixzglz2fk4evmgsiirwv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fixzglz2fk4evmgsiirwv.png" alt="A screenshot of the Google AI Studio chat interface. Two text prompts are visible: " width="800" height="606"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interactive Image Evolution in AI Studio:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AI Studio truly shines with its conversational image editing. You can refine and evolve images through simple, natural language dialogues, much like chatting with a visual collaborator.  Picture this interactive flow:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;You:&lt;/strong&gt; &lt;code&gt;Generate a picture of a classic blue muscle car from the 1960s.&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;AI (Shows you a blue muscle car image)&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;You:&lt;/strong&gt; &lt;code&gt;Make it a convertible with the top down.&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;AI (Updates the image, now a convertible muscle car)&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;You:&lt;/strong&gt; &lt;code&gt;Change the color to a vibrant, sunny yellow.&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;AI (Again, updates the image, now a yellow convertible muscle car)&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fushmvytrvhp5lsvj9sch.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fushmvytrvhp5lsvj9sch.png" alt="A sequence of three screenshots of the Google AI Studio chat interface, demonstrating conversational image editing. Top: The user's prompt " width="800" height="664"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhyl2f9920gy4iijj2t30.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhyl2f9920gy4iijj2t30.png" alt="Bottom: The user's final prompt " width="800" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This back-and-forth, where the model remembers your previous instructions and the image's history, makes image refinement remarkably intuitive and efficient. It's like having a visual assistant that understands your creative vision as it evolves.&lt;/p&gt;


&lt;h3&gt;
  
  
  2. Gemini API: Programmatic Image Power
&lt;/h3&gt;

&lt;p&gt;For developers looking to weave image generation directly into applications or automated workflows, the Gemini API offers a powerful and flexible programmatic route.  Leveraging the &lt;code&gt;google-genai&lt;/code&gt; Python library, integrating this capability is surprisingly straightforward.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic Text-to-Image Generation with Python:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, ensure you have the necessary library installed in your Python environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;!&lt;/span&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-U&lt;/span&gt; &lt;span class="nt"&gt;-q&lt;/span&gt; &lt;span class="s2"&gt;"google-genai&amp;gt;=1.5.0"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's examine a Python code snippet to generate an image programmatically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;genai&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google.genai&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;PIL&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Image&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;io&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BytesIO&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;genai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# Assumes API key is configured, e.g., via environment variable
&lt;/span&gt;
&lt;span class="n"&gt;contents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Generate a photorealistic image of a vibrant parrot perched on a tropical flower.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;models/gemini-2.0-flash-exp&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GenerateContentConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response_modalities&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Text&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Image&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inline_data&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;image_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inline_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;
    &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BytesIO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image_data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;parrot.png&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Or image.show() to display directly
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Breakdown:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;client = genai.Client()&lt;/code&gt;&lt;/strong&gt;: Initializes the Gemini client.  This assumes your API key is already set up, for instance, as an environment variable named &lt;code&gt;GOOGLE_API_KEY&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;model="models/gemini-2.0-flash-exp"&lt;/code&gt;&lt;/strong&gt;:  Specifies the experimental Gemini 2.0 Flash model for image generation using the correct model ID format for the API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;contents = "..."&lt;/code&gt;&lt;/strong&gt;:  Your text prompt describing the desired image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;config=types.GenerateContentConfig(response_modalities=['Text', 'Image'])&lt;/code&gt;&lt;/strong&gt;:  This is key! &lt;code&gt;response_modalities=['Text', 'Image']&lt;/code&gt; tells the API to expect both text and image outputs in the response.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Image Handling:&lt;/strong&gt; The code iterates through the response parts. If a part contains &lt;code&gt;inline_data&lt;/code&gt; (image data), it opens the image using PIL and saves it as "parrot.png". You could alternatively use &lt;code&gt;image.show()&lt;/code&gt; to display the image directly instead of saving it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Before running this code:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Set up your API key:&lt;/strong&gt; Ensure you have a Google AI Studio API key and have configured it.  One common method is to set the &lt;code&gt;GOOGLE_API_KEY&lt;/code&gt; environment variable. Refer to the Gemini API documentation for detailed instructions on API key setup and authentication.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Gemini 2.0 Flash in Action: Real-World Use Cases
&lt;/h2&gt;

&lt;p&gt;Gemini 2.0 Flash's native image generation isn't just a tech demo; it's a versatile toolkit with game-changing potential across numerous fields. Let's explore some compelling applications:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Cantonese Visual Storytelling: Bridging Languages with Images
&lt;/h3&gt;

&lt;p&gt;Gemini 2.0 Flash transcends language barriers, generating image sequences to accompany stories, even in languages like Cantonese.  This showcases its ability to connect text and visuals across different linguistic contexts, opening doors for culturally relevant content.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a Cantonese Children's Story:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine crafting a children's story in Cantonese about a playful panda learning Kung Fu. You could provide a Cantonese narrative and instruct Gemini 2.0 Flash to generate a series of images, one for each key scene. The model is designed to maintain consistency in characters, settings, and overall mood throughout the visual narrative.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Cantonese Prompt (for a story about a Panda learning Kung Fu):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;用廣東話寫一個關於熊貓學功夫嘅短篇故事，然後為每個關鍵場景生成一張圖片。 例如，第一張圖片可以係熊貓喺竹林入面第一次嘗試功夫動作。&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Translation: Write a short story in Cantonese about a panda learning Kung Fu, and then generate an image for each key scene. For example, the first image could be a panda in a bamboo forest trying Kung Fu moves for the first time.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;While explicit confirmation for Cantonese image generation is still emerging, Gemini's robust multilingual capabilities, including Chinese, strongly suggest this is already possible or will be soon.  This unlocks exciting opportunities for Cantonese-speaking communities to create culturally relevant educational materials, engaging children's stories, and targeted marketing campaigns that resonate deeply.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyj8ecb3pd15h58op0unw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyj8ecb3pd15h58op0unw.png" alt="A screenshot of the Google AI Studio chat interface showing a Cantonese short story about a panda learning Kung Fu. The prompt at the top is in Cantonese and translates to requesting a story about a panda learning Kung Fu with an image for each key scene. Below the Cantonese text of the story, there are five distinct images. The first image shows a panda in a bamboo forest attempting a martial arts pose." width="800" height="608"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk29ch9kh99eozm0gppac.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk29ch9kh99eozm0gppac.png" alt="The second image depicts the panda being instructed in Kung Fu by an elderly figure in traditional clothing." width="740" height="595"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnpxvlftqe6c5g85wg16x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnpxvlftqe6c5g85wg16x.png" alt="The third image shows the panda in a focused martial arts stance, seemingly practicing its moves." width="755" height="509"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxupu1dr3u05efw9ftv95.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxupu1dr3u05efw9ftv95.png" alt="The fourth image shows the panda in a dynamic martial arts pose. It is standing on one leg, with its other leg extended behind it. One arm is raised above its head, and the other is outstretched to the side, suggesting it is performing a Kung Fu move. The background depicts a lush, green environment with bamboo stalks." width="762" height="433"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2o9ls8bs23vjc8qw76ja.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2o9ls8bs23vjc8qw76ja.png" alt="The fifth image shows a confrontation scene. In the foreground, a large, pink wild boar with tusks stands facing the viewer with an aggressive expression. To its left, a panda is standing with its arms outstretched, seemingly trying to protect something. To the right of the boar, another panda is standing in a defensive martial arts stance. The background shows a dense bamboo forest." width="759" height="616"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  2. From 2D Image to 3D Model: Generating Objects for Meshy AI and Beyond
&lt;/h3&gt;

&lt;p&gt;Synthetic data generation takes an exciting turn when we combine Gemini 2.0 Flash with tools like Meshy AI.  Instead of just creating static 2D images of 3D objects, we can use Gemini 2.0 Flash to generate the &lt;em&gt;input&lt;/em&gt; for 2D-to-3D conversion services, opening up a streamlined pathway to create 3D models.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Power of Synergy: Gemini 2.0 Flash + 2D-to-3D AI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This approach leverages the strengths of both technologies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Gemini 2.0 Flash:&lt;/strong&gt; excels at generating diverse and customizable 2D images of objects from text prompts, allowing for control over style, viewpoint, and lighting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Meshy AI (or similar services):&lt;/strong&gt; specializes in reconstructing 3D models from single or multiple 2D images.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By using Gemini 2.0 Flash to create the 2D image, we gain a powerful and flexible way to generate the precise visual input needed for 3D model creation in tools like Meshy AI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Workflow: Image to 3D Model in a Few Steps&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 1. &lt;strong&gt;Generate a 2D Image with Gemini 2.0 Flash:&lt;/strong&gt; Use a text prompt to describe the 3D object and desired viewpoint.  Focus on creating a clear, well-defined image from a single perspective that will be easily interpreted by a 2D-to-3D tool.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example Prompts for 2D Image Generation (for 3D conversion):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;"Render a photorealistic image of a classic red sports car from a standard side view with plain background."&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(Focus on a clear side view for easier 3D reconstruction)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa8dl1610valpknt2ex2k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa8dl1610valpknt2ex2k.png" alt="A photorealistic image of a classic red sports car from a standard side view with plain background" width="800" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Step 2. &lt;strong&gt;Download the Generated Image:&lt;/strong&gt; Save the image generated by Gemini 2.0 Flash to your computer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 3. &lt;strong&gt;Upload to &lt;a href="https://www.meshy.ai/" rel="noopener noreferrer"&gt;Meshy AI&lt;/a&gt; (or a similar 2D-to-3D service):&lt;/strong&gt; Visit the &lt;a href="https://www.meshy.ai/" rel="noopener noreferrer"&gt;Meshy AI&lt;/a&gt; website (or your chosen 2D-to-3D conversion platform) and upload the 2D image you just generated.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F97mnm79478bbk1s7kyb8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F97mnm79478bbk1s7kyb8.png" alt="A screenshot of the Meshy AI website interface. The user is on the " width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 4. &lt;strong&gt;Generate the 3D Model:&lt;/strong&gt; Follow the instructions on the Meshy AI platform to initiate the 2D-to-3D conversion process. Meshy AI will analyze the 2D image and reconstruct a 3D model based on its interpretation of the visual information.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg47h7errwtzasarnr54v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg47h7errwtzasarnr54v.png" alt="A screenshot of the Meshy AI website interface showing the result of the 2D-to-3D conversion. A 3D model of the red classic sports car is displayed in the center, with controls for rotating and viewing it from different angles. Information about the model's topology (Triangles, Faces, Vertices) is visible on the left sidebar." width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 5. &lt;strong&gt;Download and Use the 3D Model:&lt;/strong&gt; Once Meshy AI has processed the image, you can typically download the generated 3D model in a common 3D file format (like .obj or .glb) and use it in your 3D projects, game development, virtual environments, or other applications.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  3. OCR Training Data: Enhancing Text Recognition with Synthetic Handwriting
&lt;/h3&gt;

&lt;p&gt;Optical Character Recognition (OCR) systems rely on vast datasets of text images for effective training. Gemini 2.0 Flash can generate images of handwritten text, providing invaluable synthetic data to improve OCR accuracy and robustness.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Addressing the Challenges of Handwriting Recognition:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Real-world handwriting is incredibly varied. Synthetic handwriting data can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Capture Style Diversity:&lt;/strong&gt; Generate samples encompassing a wide range of handwriting styles, slants, pressure variations, and letter formations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simulate Real-World Imperfections:&lt;/strong&gt; Introduce realistic noise, smudges, and variations to mimic the imperfections found in real handwritten documents.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reduce Bias:&lt;/strong&gt; Address potential biases in existing datasets by generating samples of underrepresented handwriting styles, leading to more inclusive and accurate OCR systems.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example Prompts for Handwriting Data Generation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;"Generate an image of the sentence: 'The quick brown fox jumps over the lazy dog.' written in elegant cursive handwriting."&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ajdjr4f55mylvltifg7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ajdjr4f55mylvltifg7.png" alt="A screenshot of the Google AI Studio chat interface. The prompt at the top reads: " width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;"Generate multiple images of the English word 'Example' written in various messy and hurried handwriting styles."&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxdf22w5mq5o4af970740.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxdf22w5mq5o4af970740.png" alt="A screenshot of the Google AI Studio chat interface. The prompt at the top reads: " width="800" height="715"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This capability is particularly critical for developing more accurate and reliable OCR models for processing historical documents, handwritten notes, and multilingual text recognition tasks, where handwriting variability is a significant challenge.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion: Visual AI is Here
&lt;/h2&gt;

&lt;p&gt;Gemini 2.0 Flash's native image generation is a significant leap, democratizing visual creation through accessible tools like AI Studio and the API. This technology has the power to transform content creation, accessibility, and AI research. Experiment and explore the future of AI visuals with Gemini 2.0 Flash.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>google</category>
      <category>gemini</category>
    </item>
    <item>
      <title>Building a Simple Stock Insights Agent with Gemini 2.0 Flash on Vertex AI</title>
      <dc:creator>Thomas Chong</dc:creator>
      <pubDate>Sun, 02 Mar 2025 18:40:24 +0000</pubDate>
      <link>https://dev.to/thomas-chong/building-a-stock-insights-agent-with-gemini-20-flash-on-vertex-ai-5bkl</link>
      <guid>https://dev.to/thomas-chong/building-a-stock-insights-agent-with-gemini-20-flash-on-vertex-ai-5bkl</guid>
      <description>&lt;h2&gt;
  
  
  Introduction to Gemini 2.0 Flash
&lt;/h2&gt;

&lt;p&gt;Gemini 2.0 Flash, Google's efficient generative AI model, excels at tasks requiring low latency and strong performance. Its agentic capabilities, large context window, and native tool use make it ideal for finance applications. In this blog, we'll create a stock insights agent using Gemini 2.0 Flash on Vertex AI, leveraging Google Search grounding for real-time data. We'll use the &lt;code&gt;google-genai&lt;/code&gt; library and build a Gradio UI for interactive stock analysis.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step-by-Step Guide: Developing the Stock Insights Agent
&lt;/h2&gt;

&lt;p&gt;Let’s break down the process into environment setup, connecting to Gemini 2.0 Flash, integrating Google Search, and creating a Gradio web app.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Environment Setup and Authentication
&lt;/h3&gt;

&lt;p&gt;Before coding, prepare your Google Cloud environment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Enable Vertex AI and Vertex AI Search and Conversation APIs&lt;/strong&gt; in your Google Cloud project. Ensure you have the necessary permissions.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Install the Google Gen AI SDK for Python&lt;/strong&gt;: &lt;code&gt;pip install --upgrade google-genai&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Authentication&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If running this notebook on Google Colab, you will need to authenticate your environment. To do this, run the new cell below.
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight python"&gt;&lt;code&gt;  &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;google.colab&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;# Authenticate user to Google Cloud
&lt;/span&gt;      &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google.colab&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;auth&lt;/span&gt;

      &lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;authenticate_user&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;ul&gt;
&lt;li&gt;Otherwise, set up authentication using a service account. Go to the Google Cloud Console, create a service account with Vertex AI access, and download its JSON key. Then set the &lt;code&gt;GOOGLE_APPLICATION_CREDENTIALS&lt;/code&gt; environment variable to the path of this JSON key to authenticate your API calls. For example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;GOOGLE_APPLICATION_CREDENTIALS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/path/to/your/service-account-key.json"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initialize the Vertex AI SDK&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;  &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
  &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;genai&lt;/span&gt;

  &lt;span class="n"&gt;PROJECT_ID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;[your-gcp-project-id]&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# Replace with your GCP project ID
&lt;/span&gt;  &lt;span class="n"&gt;LOCATION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;us-central1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# Or the region where Gemini 2.0 Flash is available
&lt;/span&gt;
  &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;genai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vertexai&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;project&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;PROJECT_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;LOCATION&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Connecting to the Gemini 2.0 Flash Model
&lt;/h3&gt;

&lt;p&gt;Connect to the Gemini 2.0 Flash model using the &lt;code&gt;google-genai&lt;/code&gt; library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;MODEL_ID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;gemini-2.0-flash-001&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# Or the latest available model ID
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Enabling Google Search Grounding for Real-Time Stock Data
&lt;/h3&gt;

&lt;p&gt;Use Google Search to ground responses with up-to-date information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google.genai.types&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;GoogleSearch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Tool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;GenerateContentConfig&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="c1"&gt;# Create a Google Search retrieval tool
&lt;/span&gt;&lt;span class="n"&gt;google_search_tool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Tool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;google_search&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;GoogleSearch&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="c1"&gt;# Define our query or prompt for the model
&lt;/span&gt;&lt;span class="n"&gt;company_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alphabet Inc.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# example company
&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Provide information about &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;company_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; in JSON format with the following keys:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;- `company_info`: A brief overview of the company and what it does.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;- `recent_news`: An array of 2-3 recent news items about the company, each with a `title` and a short `description`.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;- `trend_prediction`: An analysis of the stock trend and a suggested target price for the near future.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Configure the model to use the Google Search tool
&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GenerateContentConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;google_search_tool&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# Generate a response using the model, grounded with Google Search data
&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;MODEL_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Print nicely formatted JSON
&lt;/span&gt;&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSONDecodeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Error decoding JSON:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Testing the Model Response (Example)
&lt;/h3&gt;

&lt;p&gt;Test the model with a sample query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;user_question&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Provide information about Tesla in JSON format with the following keys:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;- `company_info`: A brief overview of the company and what it does.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;- `recent_news`: An array of 2-3 recent news items about the company, each with a `title` and a short `description`.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;- `trend_prediction`: An analysis of the stock trend and a suggested target price for the near future.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GenerateContentConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;google_search_tool&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;MODEL_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;user_question&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Print nicely formatted JSON
&lt;/span&gt;&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSONDecodeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Error decoding JSON:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Gradio UI Implementation
&lt;/h3&gt;

&lt;p&gt;Create an interactive web interface with Gradio:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;gradio&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;gr&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="c1"&gt;# Function to get stock insights for a given company name
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_stock_insights&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;company&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;company&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;

    &lt;span class="c1"&gt;# Construct a prompt for the LLM to generate a structured JSON response
&lt;/span&gt;    &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Provide information about &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;company&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; in JSON format with the following keys:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;- `company_info`: A brief overview of the company and what it does.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;- `recent_news`: An array of 2-3 recent news items about the company, each with a `title` and a short `description`.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;- `trend_prediction`: An analysis of the stock trend and a suggested target price for the near future.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Use the Vertex AI model with Google Search grounding
&lt;/span&gt;    &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GenerateContentConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;google_search_tool&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;MODEL_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;company_info&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;company_info&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;news_items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;recent_news&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;
        &lt;span class="n"&gt;news_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;news&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;news_items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;news_string&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;- **&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;news&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;title&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;No Title&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;**: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;news&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;description&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;No Description&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="n"&gt;trend_prediction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;trend_prediction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;company_info&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;news_string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;trend_prediction&lt;/span&gt;

    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JSONDecodeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Error decoding JSON. Please try again.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;


&lt;span class="c1"&gt;# Define Gradio interface components
&lt;/span&gt;&lt;span class="n"&gt;company_input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Textbox&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Company or Stock Ticker&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;e.g. Tesla or TSLA&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;company_info_output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Markdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Company Information&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;news_info_output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Markdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Recent News&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;prediction_info_output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Markdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Trend &amp;amp; Prediction&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Create the Gradio interface
&lt;/span&gt;&lt;span class="n"&gt;demo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Interface&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;get_stock_insights&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;company_input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;outputs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;company_info_output&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;news_info_output&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prediction_info_output&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;📊 Stock Insights Agent&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Enter a company name or stock ticker to get an overview, latest news, and an AI-driven stock trend prediction.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Launch the app (if running locally, this will start a web server for the UI)
&lt;/span&gt;&lt;span class="n"&gt;demo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prompt:&lt;/strong&gt; The prompt now explicitly requests the output in JSON format, specifying the structure with keys &lt;code&gt;company_info&lt;/code&gt;, &lt;code&gt;recent_news&lt;/code&gt; (an array of &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;description&lt;/code&gt; objects), and &lt;code&gt;trend_prediction&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JSON Parsing:&lt;/strong&gt; The &lt;code&gt;get_stock_insights&lt;/code&gt; function now parses the JSON response using &lt;code&gt;json.loads()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gradio Output Population:&lt;/strong&gt; The function extracts the values from the parsed JSON and populates the corresponding Gradio output components (&lt;code&gt;company_info&lt;/code&gt;, &lt;code&gt;news_string&lt;/code&gt;, &lt;code&gt;trend_prediction&lt;/code&gt;).  The &lt;code&gt;news_string&lt;/code&gt; is formatted into a Markdown list for better presentation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling:&lt;/strong&gt; Includes a &lt;code&gt;try...except&lt;/code&gt; block to handle potential &lt;code&gt;JSONDecodeError&lt;/code&gt; if the model doesn't return a valid JSON.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let's move the GitHub repo structure before the conclusion:&lt;/p&gt;

&lt;h2&gt;
  
  
  GitHub Repository Structure
&lt;/h2&gt;

&lt;p&gt;When packaging this project for collaboration and deployment, a clear repository structure is essential. Here’s a suggested structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stock-insights-agent/
├── README.md
├── requirements.txt
├── app.py
├── .env (optional)
├── utils/
│   ├── __init__.py
│   └── data_utils.py
└── notebooks/
    └── development.ipynb (optional)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;README.md&lt;/strong&gt;: Project overview, setup, dependencies, API credentials, and usage examples.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;requirements.txt&lt;/strong&gt;: Pinned Python libraries (e.g., &lt;code&gt;google-genai&lt;/code&gt;, &lt;code&gt;gradio&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;app.py&lt;/strong&gt;: Main application script (Vertex AI initialization, Gradio interface).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;.env (optional)&lt;/strong&gt;: Environment variables (API keys, project IDs).  &lt;strong&gt;Important&lt;/strong&gt;:  Add to &lt;code&gt;.gitignore&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;utils/&lt;/strong&gt;: Helper functions (prompt construction, result parsing).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;notebooks/&lt;/strong&gt;: Jupyter notebook for development/prototyping (optional).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ensure the &lt;strong&gt;README&lt;/strong&gt; includes instructions for Google Cloud credentials and API enablement. Document the agent's functionality and any limitations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we built a Stock Insights Agent using Gemini 2.0 Flash on Vertex AI. By leveraging Google Search grounding and the &lt;code&gt;google-genai&lt;/code&gt; library, we created an interactive Gradio interface for real-time stock analysis.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next steps:&lt;/strong&gt; Consider deploying the Gradio app, integrating robust data sources, refining prompts, or enabling multi-turn conversations. Gemini 2.0 Flash offers many possibilities in finance, from personalized assistants to automated report generators.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
