mariusjabami commited on
Commit
a174543
·
verified ·
1 Parent(s): bf21e34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -1,25 +1,28 @@
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
3
  import threading
 
4
 
5
- # Inicializar o modelo e o tokenizer
6
- model_name = "lambdaindie/lambda-1v-1B" # Troca com o nome do modelo que estás a usar
7
- model = AutoModelForCausalLM.from_pretrained(model_name).to("cuda") # Usando GPU (ou "cpu" se não tiveres GPU)
 
 
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
 
10
  stop_flag = {"stop": False}
11
 
 
12
  def respond(prompt, history):
13
  stop_flag["stop"] = False
14
 
15
- # Prompt modificado conforme solicitado
16
  full_prompt = f"\nThink a bit step-by-step before answering. \nQuestion: {prompt} \nAnswer:"
17
-
18
- inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
19
 
20
  streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
21
 
22
- # Thread para geração de texto
23
  generation_thread = threading.Thread(
24
  target=model.generate,
25
  kwargs={
@@ -40,27 +43,29 @@ def respond(prompt, history):
40
  if stop_flag["stop"]:
41
  return "", history
42
  reasoning += new_text
43
- yield "", history[:-1] + [(prompt, f"<div class='final-answer'>{reasoning}</div>")]
44
 
 
45
  def stop_generation():
46
  stop_flag["stop"] = True
47
 
48
- # Definir a interface do Gradio
49
  with gr.Blocks(css="""
50
- #chatbot, .gr-markdown, .gr-button, .gr-textbox {
51
- font-family: 'JetBrains Mono', monospace !important;
52
- font-size: 11px !important;
53
- }
54
- .final-answer {
55
- background-color: #1e1e1e;
56
- color: #ffffff;
57
- padding: 10px;
58
- border-left: 4px solid #4caf50;
59
- font-family: 'JetBrains Mono', monospace !important;
60
- white-space: pre-wrap;
61
- font-size: 11px !important;
62
  }
63
  """) as demo:
 
64
  gr.Markdown("## λambdAI — Reasoning Chat")
65
 
66
  chatbot = gr.Chatbot(elem_id="chatbot")
 
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
3
  import threading
4
+ import torch
5
 
6
+ # Detectar dispositivo automaticamente (GPU ou CPU)
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+
9
+ # Carregar modelo e tokenizer
10
+ model_name = "lambdaindie/lambda-1v-1B"
11
+ model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
12
  tokenizer = AutoTokenizer.from_pretrained(model_name)
13
 
14
  stop_flag = {"stop": False}
15
 
16
+ # Função de resposta
17
  def respond(prompt, history):
18
  stop_flag["stop"] = False
19
 
 
20
  full_prompt = f"\nThink a bit step-by-step before answering. \nQuestion: {prompt} \nAnswer:"
21
+ inputs = tokenizer(full_prompt, return_tensors="pt").to(device)
 
22
 
23
  streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
24
 
25
+ # Iniciar thread de geração
26
  generation_thread = threading.Thread(
27
  target=model.generate,
28
  kwargs={
 
43
  if stop_flag["stop"]:
44
  return "", history
45
  reasoning += new_text
46
+ yield "", (history or []) + [(prompt, f"<div class='final-answer'>{reasoning}</div>")]
47
 
48
+ # Função para parar a geração
49
  def stop_generation():
50
  stop_flag["stop"] = True
51
 
52
+ # Interface Gradio
53
  with gr.Blocks(css="""
54
+ #chatbot, .gr-markdown, .gr-button, .gr-textbox {
55
+ font-family: 'JetBrains Mono', monospace !important;
56
+ font-size: 11px !important;
57
+ }
58
+ .final-answer {
59
+ background-color: #1e1e1e;
60
+ color: #ffffff;
61
+ padding: 10px;
62
+ border-left: 4px solid #4caf50;
63
+ font-family: 'JetBrains Mono', monospace !important;
64
+ white-space: pre-wrap;
65
+ font-size: 11px !important;
66
  }
67
  """) as demo:
68
+ gr.Markdown('<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet">')
69
  gr.Markdown("## λambdAI — Reasoning Chat")
70
 
71
  chatbot = gr.Chatbot(elem_id="chatbot")