Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,67 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
-
from transformers import
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import requests
|
| 6 |
import torch
|
| 7 |
-
import io
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
model =
|
| 13 |
-
|
| 14 |
torch_dtype=torch.bfloat16,
|
| 15 |
low_cpu_mem_usage=True
|
| 16 |
)
|
| 17 |
|
| 18 |
@spaces.GPU(duration=120)
|
| 19 |
-
def generate_response(message, history
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Construye mensajes para formato chat multimodal de Gemma-3
|
| 24 |
-
messages = [{"role": "system", "content": [{"type": "text", "text": "You are a helpful assistant."}]}]
|
| 25 |
for user_msg, ai_msg in history:
|
| 26 |
-
messages.append({"role": "user", "content":
|
| 27 |
if ai_msg:
|
| 28 |
-
messages.append({"role": "model", "content":
|
| 29 |
-
|
| 30 |
-
# Añade mensaje actual
|
| 31 |
-
user_content = [{"type": "text", "text": message}]
|
| 32 |
-
if image: # Si hay imagen, la añade
|
| 33 |
-
# Carga imagen desde URL o base64 (Gradio maneja upload como PIL)
|
| 34 |
-
if isinstance(image, str): # URL
|
| 35 |
-
response = requests.get(image)
|
| 36 |
-
img = Image.open(io.BytesIO(response.content))
|
| 37 |
-
else: # PIL Image
|
| 38 |
-
img = image
|
| 39 |
-
user_content.insert(0, {"type": "image", "image": img})
|
| 40 |
-
|
| 41 |
-
messages.append({"role": "user", "content": user_content})
|
| 42 |
|
| 43 |
# Aplica template de chat
|
| 44 |
-
|
| 45 |
messages,
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
# Genera en GPU
|
| 53 |
-
input_len = inputs["input_ids"].shape[-1]
|
| 54 |
with torch.no_grad():
|
| 55 |
-
|
| 56 |
**inputs,
|
| 57 |
max_new_tokens=150,
|
| 58 |
do_sample=True,
|
| 59 |
temperature=0.7,
|
| 60 |
-
pad_token_id=
|
| 61 |
)
|
| 62 |
-
generation = generation[0][input_len:]
|
| 63 |
|
| 64 |
# Decodifica solo tokens nuevos
|
| 65 |
-
response =
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
torch.cuda.empty_cache()
|
| 70 |
|
| 71 |
return response
|
| 72 |
|
| 73 |
-
# Interfaz chat
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
return "", chat_history
|
| 86 |
-
|
| 87 |
-
msg.submit(respond, [msg, chatbot, img], [msg, chatbot])
|
| 88 |
-
clear.click(lambda: ([], ""), None, chatbot)
|
| 89 |
-
|
| 90 |
-
gr.Examples(
|
| 91 |
-
examples=[
|
| 92 |
-
["Describe esta imagen.", "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"],
|
| 93 |
-
["Hola, ¿qué tal?", None],
|
| 94 |
-
["Explica la IA en 3 líneas.", None]
|
| 95 |
-
],
|
| 96 |
-
inputs=[msg, img]
|
| 97 |
-
)
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|
| 100 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
+
# Modelo correcto: google/gemma-3-4b-it (texto SOLO, no multimodal)
|
| 7 |
+
model_name = "google/gemma-3-4b-it"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
model_name,
|
| 11 |
torch_dtype=torch.bfloat16,
|
| 12 |
low_cpu_mem_usage=True
|
| 13 |
)
|
| 14 |
|
| 15 |
@spaces.GPU(duration=120)
|
| 16 |
+
def generate_response(message, history):
|
| 17 |
+
# Construye mensajes para formato chat de Gemma-3
|
| 18 |
+
messages = []
|
|
|
|
|
|
|
|
|
|
| 19 |
for user_msg, ai_msg in history:
|
| 20 |
+
messages.append({"role": "user", "content": user_msg})
|
| 21 |
if ai_msg:
|
| 22 |
+
messages.append({"role": "model", "content": ai_msg})
|
| 23 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Aplica template de chat
|
| 26 |
+
full_prompt = tokenizer.apply_chat_template(
|
| 27 |
messages,
|
| 28 |
+
tokenize=False,
|
| 29 |
+
add_generation_prompt=True
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Tokeniza
|
| 33 |
+
inputs = tokenizer(full_prompt, return_tensors='pt')
|
| 34 |
|
| 35 |
# Genera en GPU
|
|
|
|
| 36 |
with torch.no_grad():
|
| 37 |
+
outputs = model.generate(
|
| 38 |
**inputs,
|
| 39 |
max_new_tokens=150,
|
| 40 |
do_sample=True,
|
| 41 |
temperature=0.7,
|
| 42 |
+
pad_token_id=tokenizer.eos_token_id
|
| 43 |
)
|
|
|
|
| 44 |
|
| 45 |
# Decodifica solo tokens nuevos
|
| 46 |
+
response = tokenizer.decode(
|
| 47 |
+
outputs[0][inputs.input_ids.shape[1]:],
|
| 48 |
+
skip_special_tokens=True
|
| 49 |
+
).strip()
|
|
|
|
| 50 |
|
| 51 |
return response
|
| 52 |
|
| 53 |
+
# Interfaz chat SIMPLE (solo texto)
|
| 54 |
+
demo = gr.ChatInterface(
|
| 55 |
+
fn=generate_response,
|
| 56 |
+
title="Gemma-3-4B-IT Chat",
|
| 57 |
+
description="Chatbot con google/gemma-3-4b-it en ZeroGPU",
|
| 58 |
+
examples=[
|
| 59 |
+
["Hola, ¿cómo estás?"],
|
| 60 |
+
["Explica qué es la IA en 3 líneas."],
|
| 61 |
+
["Cuéntame un chiste"]
|
| 62 |
+
],
|
| 63 |
+
cache_examples=True
|
| 64 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
demo.launch()
|