🎨 Lesson 7.1: Using Gradio or Streamlit to Build Frontend Interfaces for AI Agents
🎯 Lesson Objective
By the end of this lesson, you will:
-
Understand the difference between Gradio and Streamlit
-
Build a simple chat UI that connects to an AI agent
-
Enable text or audio input and dynamic responses
-
Learn how to integrate file upload, memory, and user sessions
-
Prepare your AI agent for public or internal use
🧰 1. Why Use Gradio or Streamlit?
Both tools allow you to wrap AI agents in a user-friendly web interface, without needing deep frontend experience.
| Feature | Gradio | Streamlit |
|---|---|---|
| UI Style | Pre-built widgets (like Hugging Face demos) | Customizable Python-based layout |
| Ease of Use | Very simple for demos | More flexible for complex apps |
| Hosting | HF Spaces, local, or cloud | Local, Streamlit Cloud, or custom |
| Input Types | Text, audio, file, image, video | Mostly text, file, image |
| Chat Interface | Native chatbot mode | Needs a bit more manual setup |
⚙️ 2. Project Setup
# For Gradio
pip install gradio langchain openai
# For Streamlit
pip install streamlit langchain openai
Optional: Whisper (for audio), TTS, etc.
💬 3. Creating a Simple Chat UI with Gradio
✅ Goal: Create a web chat app for your LangChain agent
# gradio_chat.py
import gradio as gr
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
llm = ChatOpenAI()
memory = ConversationBufferMemory()
conversation = ConversationChain(llm=llm, memory=memory)
def chat_with_agent(message, history):
response = conversation.run(message)
history.append((message, response))
return history, history
gr.ChatInterface(
fn=chat_with_agent,
title="🧠 Your AI Assistant",
description="Chat with your custom AI agent.",
).launch()
🎯 Features:
-
Keeps conversation memory
-
Easily handles file uploads, audio input via Gradio widgets
-
Can be deployed with
.launch(share=True)to get a public link
🌐 4. Creating a Chat App with Streamlit
# streamlit_chat.py
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
st.set_page_config(page_title="AI Agent Chat", layout="centered")
st.title("🤖 Chat with your AI Agent")
llm = ChatOpenAI()
if "memory" not in st.session_state:
st.session_state.memory = ConversationBufferMemory()
st.session_state.conversation = ConversationChain(
llm=llm,
memory=st.session_state.memory
)
user_input = st.text_input("Ask me anything:")
if user_input:
response = st.session_state.conversation.run(user_input)
st.write("💬 You:", user_input)
st.write("🧠 Agent:", response)
💡 Add-ons:
-
Use
st.file_uploader()to upload PDFs, images -
Add charts with
plotly,altair, ormatplotlib -
Use
st.audio()for audio input/output -
Enable session-based memory across chats
🎙️ 5. Adding Audio Input (Optional)
Gradio:
def transcribe_audio(audio):
import whisper
model = whisper.load_model("base")
result = model.transcribe(audio)
return result['text']
gr.Interface(fn=transcribe_audio, inputs="microphone", outputs="text").launch()
Streamlit:
audio_file = st.file_uploader("Upload audio", type=["wav", "mp3"])
if audio_file:
# Save, transcribe, and pass to LLM
...
🔐 6. Managing Users, Sessions, and Memory
-
Gradio: Use
stateand session IDs to manage memory per user -
Streamlit: Use
st.session_stateto store chat history, memory -
LangChain: Use
ConversationBufferMemory,VectorStoreMemory, or custom storage (SQLite, Redis)
🔌 7. Connecting External Tools (APIs, PDFs, DBs)
You can modify your chat_with_agent() or conversation.run() functions to include:
-
File parsing (PDF, DOCX, etc.)
-
Database querying tools
-
Custom search agents
-
Multi-agent orchestration
This is often done with:
from langchain.agents import initialize_agent, Tool
🚀 8. Deployment Options
| Method | Description |
|---|---|
| Gradio Launch | launch(share=True) gives public URL instantly |
| Streamlit Cloud | Deploy for free at streamlit.io |
| Docker | Package your app for production |
| Render/Heroku | Simple cloud deployment |
✅ Summary
| Topic | Key Takeaways |
|---|---|
| Gradio | Great for demos, fast UI, built-in chatbot features |
| Streamlit | More control, better for dashboards or advanced apps |
| Integration with LangChain | Wrap your AI agent logic easily |
| Audio/Text Input | Both frameworks support dynamic inputs |
| Deployment | Easy to deploy and scale for users |
84
