Sheikh Mohammad Rakib commited on
Commit ·
b66f335
1
Parent(s): 4cf3a0f
feat(tutor): replace demo with multimodal Gemma 4 chat tutor
Browse files- Initialize google-genai client and define Tutori persona system prompt
- Add chat_with_gemma handler that uploads files and calls gemma-4-31b-it
- Move GPU warm-up inside the handler per ZeroGPU requirements
- Switch UI from gr.Interface to gr.ChatInterface with multimodal=True
- Restore trailing newline at end of file
app.py
CHANGED
|
@@ -1,14 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
import torch
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
@spaces.GPU
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
+
import os
|
| 5 |
+
from google import genai
|
| 6 |
+
from google.genai import types # NEW: Import types for configuration
|
| 7 |
|
| 8 |
+
# Initialize the Google GenAI client.
|
| 9 |
+
client = genai.Client()
|
| 10 |
+
|
| 11 |
+
# Define your tutor's persona here!
|
| 12 |
+
TUTOR_SYSTEM_PROMPT = """
|
| 13 |
+
You are 'Tutori', an expert, patient, and highly technical whiteboard tutor.
|
| 14 |
+
Your goal is to educate the user step-by-step.
|
| 15 |
+
Never give just the answer; always explain the underlying concepts clearly and simply.
|
| 16 |
+
If a user uploads an image, analyze it carefully and explain it as if drawing on a whiteboard.
|
| 17 |
+
"""
|
| 18 |
|
| 19 |
@spaces.GPU
|
| 20 |
+
def chat_with_gemma(message, history):
|
| 21 |
+
"""
|
| 22 |
+
Processes the user's message and files, sending them to the Gemma 4 API with a system prompt.
|
| 23 |
+
"""
|
| 24 |
+
# 1. Verify the GPU is active (ZeroGPU requirement)
|
| 25 |
+
zero = torch.Tensor([0]).cuda()
|
| 26 |
+
print(f"GPU allocated successfully on: {zero.device}")
|
| 27 |
+
|
| 28 |
+
# 2. Extract text and files from the Gradio multimodal message
|
| 29 |
+
user_text = message["text"]
|
| 30 |
+
uploaded_files = message.get("files", [])
|
| 31 |
+
|
| 32 |
+
# We will store the text and files in a list to send to the model
|
| 33 |
+
contents = [user_text]
|
| 34 |
+
|
| 35 |
+
# 3. Process any files the user uploaded
|
| 36 |
+
for file_path in uploaded_files:
|
| 37 |
+
gemma_file = client.files.upload(file=file_path)
|
| 38 |
+
contents.append(gemma_file)
|
| 39 |
+
|
| 40 |
+
# 4. Generate a response using the multimodal Gemma 4 model with our System Instruction
|
| 41 |
+
response = client.models.generate_content(
|
| 42 |
+
model='gemma-4-31b-it',
|
| 43 |
+
contents=contents,
|
| 44 |
+
config=types.GenerateContentConfig(
|
| 45 |
+
system_instruction=TUTOR_SYSTEM_PROMPT,
|
| 46 |
+
temperature=0.7, # Optional: slightly higher temperature for creative explanations
|
| 47 |
+
)
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
return response.text
|
| 51 |
+
|
| 52 |
+
# Build the user interface
|
| 53 |
+
demo = gr.ChatInterface(
|
| 54 |
+
fn=chat_with_gemma,
|
| 55 |
+
multimodal=True,
|
| 56 |
+
title="🧠 Gemma 4 Multimodal Tutor",
|
| 57 |
+
description="Upload an image, audio clip, or video, and ask me a question! (Powered by Google AI Studio & ZeroGPU)"
|
| 58 |
+
)
|
| 59 |
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|