<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged gradio</title>
<link>https://coderlegion.com/tag/gradio</link>
<description>Powered by Question2Answer</description>
<item>
<title>Voice-Pro: Your Voice, Reimagined Across Languages</title>
<link>https://coderlegion.com/1469/voice-pro-your-voice-reimagined-across-languages</link>
<description>&lt;p&gt;Imagine a world where your voice can speak any language fluently, without ever stepping into a recording booth. Meet &lt;strong&gt;Voice-Pro&lt;/strong&gt;—a groundbreaking tool that harnesses advanced voice cloning technology to create seamless multilingual dubbing. Whether you’re a filmmaker breaking into global markets, a business reaching diverse audiences, or a creator pushing the boundaries of storytelling, Voice-Pro transforms how you connect through sound.&lt;/p&gt;
&lt;h4 id=&quot;what-makes-voice-pro-stand-out-&quot;&gt;What Makes Voice-Pro Stand Out?&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Lifelike Voice Cloning&lt;/strong&gt;: Captures the essence of your voice—tone, emotion, and all—for authentic results.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multilingual Magic&lt;/strong&gt;: Effortlessly dubs your content into multiple languages with natural fluency.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Simple Yet Powerful&lt;/strong&gt;: Designed for ease of use, delivering professional-grade output without the complexity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Versatile Applications&lt;/strong&gt;: From entertainment to education, it’s built to adapt to your needs.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=11165980403102961775&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;Curious about the tech behind it? Check out the &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/abus-aikorea/voice-pro/blob/main/README.md&quot;&gt;GitHub repository&lt;/a&gt; for a detailed README, including setup instructions. Voice-Pro runs on Python and can be deployed locally with just a few steps.&lt;/p&gt;
&lt;h4 id=&quot;see-voice-pro-in-action&quot;&gt;See Voice-Pro in Action&lt;/h4&gt;
&lt;p&gt;Want to witness the power of voice cloning and multilingual dubbing? These YouTube videos showcase what Voice-Pro can do:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://youtu.be/Tu2okoHY174&quot;&gt;Voice-Pro Demo 1&lt;/a&gt;&lt;/strong&gt;: A peek into the core voice cloning tech and its stunning realism.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://youtu.be/Wfo7vQCD4no&quot;&gt;Voice-Pro Demo 2&lt;/a&gt;&lt;/strong&gt;: Real-world examples of multilingual dubbing that redefine global content creation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;why-voice-pro-matters&quot;&gt;Why Voice-Pro Matters&lt;/h4&gt;
&lt;p&gt;In a world where communication knows no borders, Voice-Pro empowers you to amplify your voice like never before. Stay tuned for updates as we continue to refine this game-changing technology—because your story deserves to be heard, everywhere.&lt;/p&gt;
</description>
<category>Deep Learning</category>
<guid isPermaLink="true">https://coderlegion.com/1469/voice-pro-your-voice-reimagined-across-languages</guid>
<pubDate>Fri, 14 Mar 2025 20:17:12 +0000</pubDate>
</item>
<item>
<title>Deploy an AI journalist chatbot with Gradio and Supabase</title>
<link>https://coderlegion.com/291/deploy-an-ai-journalist-chatbot-with-gradio-and-supabase</link>
<description>&lt;h2&gt;1. Introduction&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;supabase-ai-chat&lt;/strong&gt; is natively developed as a Gradio-backed HuggingFace (HF) space and can be found &lt;a rel=&quot;nofollow&quot; href=&quot;https://huggingface.co/spaces/as-cle-bert/supabase-ai-chat&quot;&gt;here&lt;/a&gt;: it was developed to serve as a knowledgeable assistant in the field of AI news.&lt;/p&gt;
&lt;div class=&quot;div-green&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Want something more?&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt; There is also a version built on top of Flowise but it is only a test deployment and, due to security reasons, its access is limited: you can request the access writing a private message on CoderLegion in which you explain who you are and why you would want to use the application.&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;It is an application that exploits Supabase (&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/supabase/supabase&quot;&gt;the open-source alternative to Firebase&lt;/a&gt;) as a vector store to search for context information and HF Spaces API to query and retrieve responses from Phi-3-128K.&lt;/p&gt;
&lt;p&gt;supabase-ai-chat was built selecting 5000 AI-related news articles from &lt;a rel=&quot;nofollow&quot; href=&quot;https://huggingface.co/datasets/siavava/ai-tech-articles&quot;&gt;ai-tech-articles&lt;/a&gt; dataset on HF Hub, with the help of Supabase as a vector store (if you don't know what vectore stores are, here is an article that I wrote about them: &lt;a rel=&quot;nofollow&quot; href=&quot;https://astrabert.github.io/hophop-science/Vector-databases-explained/&quot;&gt;&quot;Vector databases explained&quot;&lt;/a&gt;). Let's break dow the steps we need to take to build this application.&lt;/p&gt;
&lt;h2&gt;2. Pre-requisites&lt;/h2&gt;
&lt;h3&gt;2a. Supabase&lt;/h3&gt;
&lt;p&gt;Before we start building our application, we will need &lt;strong&gt;to access Supabase&lt;/strong&gt;: you can head over to &lt;a rel=&quot;nofollow&quot; href=&quot;https://supabase.com/&quot;&gt;their website&lt;/a&gt; and register through your GitHub account (which is definitely the simplest way to get access to their services).&lt;/p&gt;
&lt;p&gt;Once your account has been created, you can go to &lt;em&gt;&quot;Dashboard&quot;&lt;/em&gt; and select &lt;em&gt;&quot;New Project&quot;&lt;/em&gt;. This will take you to the actual project you can build and it will require you to name it and save a password: &lt;strong&gt;do not lose the password&lt;/strong&gt;, because it will be useful later on. &lt;/p&gt;
&lt;p&gt;After Supabase manages all the dependencies of your project, you will notice that there will be both an &lt;strong&gt;API Key&lt;/strong&gt; and a &lt;strong&gt;Project URL&lt;/strong&gt; that pop up as soon as you enter the page: they won't be useful to us for this project, but they can definitely be in the future.&lt;/p&gt;
&lt;p&gt;Now make sure to go to &lt;strong&gt;&quot;Project Settings &amp;gt; Database&quot;&lt;/strong&gt; and copy the &lt;em&gt;Connection String&lt;/em&gt;: store it somewhere, for now, as it will be fundamental to build the application.&lt;/p&gt;
&lt;h3&gt;2b. HuggingFace&lt;/h3&gt;
&lt;p&gt;In order to get  an &lt;a rel=&quot;nofollow&quot; href=&quot;https://huggingface.co/&quot;&gt;Hugging Face&lt;/a&gt; account:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Go to Hugging Face from the link above and click on the &lt;strong&gt;Sign up&lt;/strong&gt; button. Alternatively, you can directly follow &lt;a rel=&quot;nofollow&quot; href=&quot;https://huggingface.co/join&quot;&gt;this link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Provide e-mail address and password&lt;/li&gt;
&lt;li&gt;Follow the instructions as you are prompted by the registration procedure&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;3. Folder Setup&lt;/h2&gt;
&lt;p&gt;You will need to have a specific folder setup in order to build this application. You can achieve that setup by cloning the &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/AstraBert/supabase-ai-chat&quot;&gt;GitHub repository&lt;/a&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;git clone https://github.com/AstraBert/supabase-ai-chat.git
cd supabase-ai-chat
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let's now take a look at the repository:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;./
├── README.md
├── app.py
├── .env.example
├── data
│   └── ainews_5k.csv.gz
├── requirements.txt
├── supabase_upsert.py
└── utils.py
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;app.py&lt;/strong&gt;, which is the script we will be using to design our application&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;utils.py&lt;/strong&gt;, which is the script where we will be storing useful classes and functions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;supabase_upsert.py&lt;/strong&gt;, which is the script that we will upload our AI news to our Supabase project with&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;requirements.txt&lt;/strong&gt;, which contains the needed dependencies for this project&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;data/ainews_5k.csv.gz&lt;/strong&gt;, which is the compressed version of the file that stores all the 5000 news articles about AI&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;.env.example&lt;/strong&gt;, which is the example file to store environment variables&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can set our environment up by running:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;python3 -m pip install -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And, on the other hand, we complete the configuration by renaming the &lt;strong&gt;.env.example&lt;/strong&gt; file to &lt;strong&gt;.env&lt;/strong&gt; and by substituting to the placeholder string associated with &lt;em&gt;supabase_db&lt;/em&gt; environment variable. An example of that would be:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;supabase_db=postgresql://postgres.fsaosjjgbgpbsiag:*Emails are not allowed*:5432/postgres
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Remember that it is important to write &lt;strong&gt;postgresql&lt;/strong&gt; at the beginning of the URL, and not only &lt;strong&gt;postgres&lt;/strong&gt;, as the newest versions of &lt;em&gt;vecs&lt;/em&gt;, the python package that serves as a PostgresSQL client, do not support this last variation of the baseurl. &lt;/p&gt;
&lt;h2&gt;4. Prepare Supabase Collection&lt;/h2&gt;
&lt;p&gt;Now that we have everything set up in terms of environment, we can upload the documents to our Supabase project, configuring them as a collection. We will be doing it using the &lt;strong&gt;supabase_upsert.py&lt;/strong&gt; script we downloaded earlier from GitHub.&lt;/p&gt;
&lt;p&gt;Let's take a quick look at the code!&lt;/p&gt;
&lt;p&gt;We start by importing the needed dependencies and loading the environment variable related to our Supabase database:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import vecs
from dotenv import load_dotenv
import os

load_dotenv()
DB_CONNECTION = os.getenv(&quot;supabase_db&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we move on to connect to Supabase client and create a new collection called &lt;em&gt;documents&lt;/em&gt;, which will be receiving vectors embedding of a size of 384:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;vx = vecs.create_client(DB_CONNECTION)
docs = vx.get_or_create_collection(name=&quot;documents&quot;, dimension=384)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we load the word-embedding model and proceed with the upsertion of the AI news to the Supabase collection (we need to read the csv, first):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import pandas as pd
df = pd.read_csv(&quot;data/ainews_5k.csv.gz&quot;)
idex = list(df[&quot;id&quot;])
texts = list(df[&quot;text&quot;])
data = [{'id': idex[i], 'content': texts[i], 'embedding': []} for i in range(len(texts))]

from sentence_transformers import SentenceTransformers
encoder = SentenceTransformer(&quot;all-MiniLM-L6-v2&quot;)

for item in data:
    docs.upsert(records=[(item['id'],encoder.encode(item['content']).tolist(), {&quot;Content&quot;: item['content']}),])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The last, but not least, portion of the code creates a vector index to ease the search for our retrieval algorithm:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;docs.create_index(measure=vecs.IndexMeasure.cosine_distance)
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;5. Define Utilities&lt;/h2&gt;
&lt;p&gt;After having upserted the AI news to Supabase, we just need to define two classes that will help us managing the queries from the users. &lt;/p&gt;
&lt;p&gt;The first one involves the possibility of translating the user's prompt from the original language to English, and then the response from English back to the original language:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;from langdetect import detect
from deep_translator import GoogleTranslator

class Translation:
    def __init__(self, text, destination):
        self.text = text
        self.destination = destination
        try:
            self.original = detect(self.text)
        except Exception as e:
            self.original = &quot;auto&quot;
    def translatef(self):
        translator = GoogleTranslator(source=self.original, target=self.destination)
        translation = translator.translate(self.text)
        return translation
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see, we use two python packages, &lt;strong&gt;langdetect&lt;/strong&gt; and &lt;strong&gt;deep_translator&lt;/strong&gt;, to achieve the goal. &lt;/p&gt;
&lt;p&gt;The second class allows us to search a Supabase collection using a text query (which is transformed into vectors on the fly by the same encoder we used to upload everything to the collection).&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class NeuralSearcher:
    def __init__(self, collection, encoder):
        self.collection = collection
        self.encoder = encoder
    def search(self, text):
        results = self.collection.query(
            data=self.encoder.encode(text).tolist(),  # required
            limit=1,                     # number of records to return
            filters={},                  # metadata filters
            measure=&quot;cosine_distance&quot;,   # distance measure to use
            include_value=True,         # should distance measure values be returned?
            include_metadata=True,      # should record metadata be returned?
        )
        return results
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For this simple example project, we do not define filters: this can be done for more advanced projects and more complex necessities.&lt;/p&gt;
&lt;h2&gt;6. Build the Application&lt;/h2&gt;
&lt;p&gt;Now that we have everything set up, we can actually build our Gradio application that will be hosted in HF Spaces. &lt;/p&gt;
&lt;p&gt;We open &lt;strong&gt;app.py&lt;/strong&gt; and start by importing packages and defining constants:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import gradio as gr
from utils import Translation, NeuralSearcher
from gradio_client import Client
import os
import vecs
from dotenv import load_dotenv
from sentence_transformers import SentenceTransformer

load_dotenv()

collection_name = &quot;documents&quot;
encoder = SentenceTransformer(&quot;all-MiniLM-L6-v2&quot;)
client = os.getenv(&quot;supabase_db&quot;)
api_client = Client(&quot;eswardivi/Phi-3-mini-128k-instruct&quot;)
lan = &quot;en&quot;
vx = vecs.create_client(client)
docs = vx.get_or_create_collection(name=collection_name, dimension=384)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we have defined our Supabase and Gradio API clients, we can finally build a function that takes the user query, recognizes the language, performs various translation tasks, retrieves some relevant information from Supabase collection, feeds them as context to Phi-3-128K along with the actual message from the user, and then finally gets a response. You can refer to the flowchart here, for a better visualization of the process: &lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://github.com/AstraBert/supabase-ai-chat/raw/main/data/supabase.drawio.png&quot; alt=&quot;Flowchart&quot;&gt;&lt;/p&gt;
&lt;p&gt;Here is the function:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;def reply(message, history):
    global docs
    global encoder
    global api_client
    global lan
    txt = Translation(message, &quot;en&quot;)
    print(txt.original, lan)
    if txt.original == &quot;en&quot; and lan == &quot;en&quot;:
        txt2txt = NeuralSearcher(docs, encoder)
        results = txt2txt.search(message)
        response = api_client.predict(
            f&quot;Context: {results[0][2]['Content']}; Prompt: {message}&quot;,# str  in 'Message' Textbox component
            0.4,# float (numeric value between 0 and 1) in 'Temperature' Slider component
            True,# bool  in 'Sampling' Checkbox component
            512,# float (numeric value between 128 and 4096)
            api_name=&quot;/chat&quot;
        )
        return response
    elif txt.original == &quot;en&quot; and lan != &quot;en&quot;:
        txt2txt = NeuralSearcher(docs, encoder)
        transl = Translation(message, lan)
        message = transl.translatef()
        results = txt2txt.search(message)
        t = Translation(results[0][2]['Content'], txt.original)
        res = t.translatef()
        response = api_client.predict(
            f&quot;Context: {res}; Prompt: {message}&quot;,
            0.4,
            True,
            512,
            api_name=&quot;/chat&quot;
        )
        response = Translation(response, txt.original)
        return response.translatef()
    elif txt.original != &quot;en&quot; and lan == &quot;en&quot;:
        txt2txt = NeuralSearcher(docs, encoder)
        results = txt2txt.search(message)
        transl = Translation(results[0][2]['Content'], &quot;en&quot;)
        translation = transl.translatef()
        response = api_client.predict(
            f&quot;Context: {translation}; Prompt: {message}&quot;,
            0.4,
            True,
            512,
            api_name=&quot;/chat&quot;
        )
        t = Translation(response, txt.original)
        res = t.translatef()
        return res
    else:
        txt2txt = NeuralSearcher(docs, encoder)
        transl = Translation(message, lan.replace(&quot;\\&quot;,&quot;&quot;).replace(&quot;'&quot;,&quot;&quot;))
        message = transl.translatef()
        results = txt2txt.search(message)
        t = Translation(results[0][2]['Content'], txt.original)
        res = t.translatef()
        response = api_client.predict(
            f&quot;Context: {res}; Prompt: {message}&quot;,# str  in 'Message' Textbox component
            0.4,
            True,
            512,
            api_name=&quot;/chat&quot;
        )
        tr = Translation(response, txt.original)
        ress = tr.translatef()
        return ress
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, in order to build the Chat Interface, Gradio offers solutions that hardly have competitors in terms of simplicity: as a matter of facts, we actually need one line of code to define the interface and one to launch it. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;demo = gr.ChatInterface(fn=reply, title=&quot;Supabase AI Journalist&quot;)
demo.launch(server_name=&quot;0.0.0.0&quot;, share=False)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, if you run:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;python3 app.py
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From your terminal, in less than 30 seconds you will see that &lt;strong&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;http://localhost:7860&quot;&gt;http://localhost:7860&lt;/a&gt;&lt;/strong&gt; gets fills with life: our chatbot is loaded and ready to be used! We can now query it by just writing a prompt and patiently waiting for the results to come!&lt;/p&gt;
&lt;p&gt;We can also upload our repository to Spaces, or simply create a space &lt;a rel=&quot;nofollow&quot; href=&quot;https://huggingface.co/spaces&quot;&gt;here&lt;/a&gt; and paste the code we've just written in files that are named as our local version of them.&lt;/p&gt;
&lt;div class=&quot;div-red&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;WARNING:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt; I strongly suggest, for safety reasons, not to expose your .env file publicly&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;You can, alternatively, define &lt;strong&gt;secret keys in the Space settings&lt;/strong&gt;, and just import the keys by simply including:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;client = os.getenv(&quot;your-secret-key-name&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In your app.py script&lt;/p&gt;
&lt;h2&gt;7. Conclusion&lt;/h2&gt;
&lt;p&gt;We now created our first Gradio application: this is the first time we build a fully &lt;strong&gt;front-end and back-end&lt;/strong&gt; defined tool, without relying on third-party interfaces such as Telegram or Discord. &lt;/p&gt;
&lt;p&gt;Moreover, we natively integrated in our project a &lt;strong&gt;vector database&lt;/strong&gt; (Supabase) for text retrieval and a &lt;strong&gt;Large Language Model&lt;/strong&gt; (Phi-3-128K) for text generation: we did everything with &lt;strong&gt;pure python&lt;/strong&gt;: the great part is that the whole code takes &lt;strong&gt;less than 150 lines&lt;/strong&gt;!&lt;/p&gt;
&lt;p&gt;Stay tuned for the next article, in which we will conclude our bot series by talking about &lt;strong&gt;Discord bots&lt;/strong&gt;, and do not forget to live a little star in the repository!&lt;/p&gt;
</description>
<category>Python</category>
<guid isPermaLink="true">https://coderlegion.com/291/deploy-an-ai-journalist-chatbot-with-gradio-and-supabase</guid>
<pubDate>Sat, 18 May 2024 08:34:04 +0000</pubDate>
</item>
</channel>
</rss>