-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgemma3n_multimodal_chat.py
More file actions
141 lines (116 loc) · 4.67 KB
/
Copy pathgemma3n_multimodal_chat.py
File metadata and controls
141 lines (116 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import streamlit as st
import requests
import base64
from io import BytesIO
from PIL import Image
import json
import os
# Streamlit app for Gemma 3n Multimodal Chat
st.set_page_config(
page_title="Gemma 3n Multimodal Assistant",
page_icon="🤖",
layout="wide"
)
# Sidebar configuration
st.sidebar.title("Gemma 3n Configuration")
api_provider = st.sidebar.selectbox(
"Choose API Provider",
["Ollama (Local)", "Together AI", "Google AI Studio"]
)
# API configuration based on provider
if api_provider == "Ollama (Local)":
ollama_url = st.sidebar.text_input("Ollama URL", "http://localhost:11434")
model_name = st.sidebar.selectbox("Model", ["gemma3n:e4b", "gemma3n:e2b"])
elif api_provider == "Together AI":
together_api_key = st.sidebar.text_input("Together AI API Key", type="password")
model_name = "google/gemma-3n-E4B-it"
else:
google_api_key = st.sidebar.text_input("Google AI API Key", type="password")
model_name = "gemma-3n-e4b"
st.title("🤖 Gemma 3n Multimodal Assistant")
st.markdown("Upload images, record audio, or ask text questions!")
# Initialize session state
if "messages" not in st.session_state:
st.session_state.messages = []
# File uploaders
col1, col2 = st.columns(2)
with col1:
uploaded_image = st.file_uploader("Upload an image", type=['png', 'jpg', 'jpeg'])
with col2:
uploaded_audio = st.file_uploader("Upload audio", type=['mp3', 'wav', 'ogg'])
# Display uploaded image
if uploaded_image:
image = Image.open(uploaded_image)
st.image(image, caption="Uploaded Image", use_container_width=True)
# Chat interface
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# User input
if prompt := st.chat_input("Ask about the image, audio, or anything else..."):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Generate response
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
try:
if api_provider == "Ollama (Local)":
response = call_ollama_api(prompt, uploaded_image, uploaded_audio, ollama_url, model_name)
elif api_provider == "Together AI":
response = call_together_api(prompt, uploaded_image, uploaded_audio, together_api_key, model_name)
else:
response = call_google_ai_api(prompt, uploaded_image, uploaded_audio, google_api_key, model_name)
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
except Exception as e:
st.error(f"Error: {str(e)}")
def call_ollama_api(prompt, image, audio, url, model):
"""Call Ollama local API with multimodal support"""
payload = {
"model": model,
"prompt": prompt,
"stream": False
}
# Add image if provided
if image:
image_data = base64.b64encode(image.getvalue()).decode()
payload["images"] = [image_data]
# Note: Audio support coming soon in Ollama
if audio:
st.warning("Audio support coming soon to Ollama!")
response = requests.post(f"{url}/api/generate", json=payload)
return response.json().get("response", "No response")
def call_together_api(prompt, image, audio, api_key, model):
"""Call Together AI API with multimodal support"""
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
messages = [{"role": "user", "content": prompt}]
# Add image if provided
if image:
image_data = base64.b64encode(image.getvalue()).decode()
messages[0]["content"] = [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}}
]
payload = {
"model": model,
"messages": messages,
"max_tokens": 1000
}
response = requests.post("https://api.together.xyz/v1/chat/completions",
headers=headers, json=payload)
return response.json()["choices"][0]["message"]["content"]
def call_google_ai_api(prompt, image, audio, api_key, model):
"""Call Google AI Studio API with multimodal support"""
# Implementation for Google AI Studio API
# This would use the official Google AI SDK
st.info("Google AI Studio integration - use the SDK for full implementation")
return "Response from Google AI Studio (implement with official SDK)"
# Add reset button
if st.sidebar.button("Clear Chat"):
st.session_state.messages = []
st.rewrite()