Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
MODEL_NAME = "manycore-research/SpatialLM-Llama-1B"
|
6 |
+
|
7 |
+
# Carrega tokenizer e modelo
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
10 |
+
MODEL_NAME,
|
11 |
+
torch_dtype=torch.float16,
|
12 |
+
device_map="auto" # Usa GPU se disponível
|
13 |
+
)
|
14 |
+
model.eval()
|
15 |
+
|
16 |
+
# Função de geração
|
17 |
+
def generate_response(prompt, temperature=0.7, top_p=0.95, max_new_tokens=200):
|
18 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
19 |
+
with torch.no_grad():
|
20 |
+
outputs = model.generate(
|
21 |
+
**inputs,
|
22 |
+
do_sample=True,
|
23 |
+
temperature=temperature,
|
24 |
+
top_p=top_p,
|
25 |
+
max_new_tokens=max_new_tokens,
|
26 |
+
pad_token_id=tokenizer.eos_token_id
|
27 |
+
)
|
28 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
29 |
+
return response
|
30 |
+
|
31 |
+
# Interface Gradio
|
32 |
+
interface = gr.Interface(
|
33 |
+
fn=generate_response,
|
34 |
+
inputs=[
|
35 |
+
gr.Textbox(label="Prompt", placeholder="Digite algo como 'Luan invadiu a base da Hegemonia...'"),
|
36 |
+
gr.Slider(minimum=0.1, maximum=1.5, value=0.7, label="Temperature"),
|
37 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, label="Top-p"),
|
38 |
+
gr.Slider(minimum=10, maximum=512, value=200, label="Max Tokens")
|
39 |
+
],
|
40 |
+
outputs=gr.Textbox(label="Resposta do Modelo"),
|
41 |
+
title="SpatialLM - Llama 1B",
|
42 |
+
description="Modelo SpatialLM LLaMA 1B rodando com GPU no Hugging Face Spaces. Ideal pra geração de texto contextualizado e linguagem espacial. Use prompts criativos."
|
43 |
+
)
|
44 |
+
|
45 |
+
interface.launch()
|