Bhaddy392 commited on
Commit
4d558ae
Β·
verified Β·
1 Parent(s): 73484c4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +171 -0
app.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import pipeline
4
+ from thinking_btn import CUSTOM_CSS, THINKING_JS
5
+ from logo import get_logo_html
6
+
7
+ pipe = pipeline(
8
+ "text-generation",
9
+ model="Qwen/Qwen2-1.5B-Instruct",
10
+ model_kwargs={"torch_dtype": torch.float16 if torch.cuda.is_available() else torch.float32},
11
+ device_map="auto",
12
+ )
13
+
14
+ def chat(message, history):
15
+ if not message or not message.strip():
16
+ return ""
17
+ try:
18
+ if len(message) > 1000:
19
+ message = message[:1000] + "... [texto cortado]"
20
+
21
+ system_msg = (
22
+ "VocΓͺ Γ© BrainGPT β€” uma inteligΓͺncia artificial poderosa, direta e precisa. "
23
+ "Responda sempre em portuguΓͺs de forma clara, concisa e inteligente. "
24
+ "Nunca invente factos. Se nΓ£o souber, diga claramente."
25
+ )
26
+
27
+ msgs = [{"role": "system", "content": system_msg}]
28
+ for user_msg, bot_msg in history[-6:]:
29
+ if user_msg:
30
+ msgs.append({"role": "user", "content": user_msg})
31
+ if bot_msg:
32
+ msgs.append({"role": "assistant", "content": bot_msg})
33
+ msgs.append({"role": "user", "content": message})
34
+
35
+ prompt = pipe.tokenizer.apply_chat_template(
36
+ msgs, tokenize=False, add_generation_prompt=True
37
+ )
38
+
39
+ if len(prompt) > 3500:
40
+ return "Conversa muito longa. Clica em Limpar e recomeΓ§a."
41
+
42
+ result = pipe(
43
+ prompt,
44
+ max_new_tokens=280,
45
+ do_sample=True,
46
+ temperature=0.7,
47
+ top_p=0.9,
48
+ pad_token_id=pipe.tokenizer.eos_token_id,
49
+ truncation=True,
50
+ )
51
+
52
+ full = result[0]["generated_text"]
53
+ resposta = full.split("<|im_start|>assistant\n")[-1].strip()
54
+ resposta = resposta.replace("<|im_end|>", "").strip()
55
+
56
+ return resposta if resposta else "Resposta vazia. Tenta reformular."
57
+
58
+ except torch.cuda.OutOfMemoryError:
59
+ return "Memoria GPU esgotada. Clica em Limpar e tenta novamente."
60
+ except Exception as e:
61
+ return f"Erro: {str(e)[:150]}"
62
+
63
+
64
+ with gr.Blocks(
65
+ css=CUSTOM_CSS,
66
+ js=THINKING_JS,
67
+ title="BrainGPT",
68
+ theme=gr.themes.Base(
69
+ primary_hue="orange",
70
+ neutral_hue="stone",
71
+ ),
72
+ ) as demo:
73
+
74
+ gr.HTML(get_logo_html("logo.png"))
75
+
76
+ gr.HTML("""
77
+ <div id="thinking-bar" class="thinking-bar">
78
+ <div class="thinking-dots">
79
+ <span></span><span></span><span></span>
80
+ </div>
81
+ <span>THINKING...</span>
82
+ </div>
83
+ """)
84
+
85
+ chatbot = gr.Chatbot(
86
+ label="",
87
+ height=480,
88
+ show_copy_button=True,
89
+ bubble_full_width=False,
90
+ avatar_images=(None, "logo.png"),
91
+ )
92
+
93
+ with gr.Row(elem_classes="input-row"):
94
+ msg = gr.Textbox(
95
+ placeholder="Escreve a tua mensagem...",
96
+ show_label=False,
97
+ lines=1,
98
+ max_lines=5,
99
+ elem_id="msg-input",
100
+ scale=8,
101
+ )
102
+ send_btn = gr.Button(
103
+ "Enviar",
104
+ elem_id="send-btn",
105
+ variant="primary",
106
+ scale=2,
107
+ )
108
+ clear_btn = gr.Button(
109
+ "Limpar",
110
+ elem_id="clear-btn",
111
+ variant="secondary",
112
+ scale=1,
113
+ )
114
+
115
+ gr.Examples(
116
+ examples=[
117
+ "Quem es tu?",
118
+ "Explica inteligencia artificial em 3 frases",
119
+ "Diferenca entre machine learning e deep learning?",
120
+ "5 dicas para ser mais produtivo",
121
+ "Como funciona o Bitcoin?",
122
+ ],
123
+ inputs=msg,
124
+ label="Exemplos",
125
+ )
126
+
127
+ def user_submit(message, history):
128
+ if not message.strip():
129
+ return "", history
130
+ history = history + [[message, None]]
131
+ return "", history
132
+
133
+ def bot_respond(history):
134
+ if not history or history[-1][1] is not None:
135
+ return history
136
+ message = history[-1][0]
137
+ response = chat(message, history[:-1])
138
+ history[-1][1] = response
139
+ return history
140
+
141
+ msg.submit(
142
+ user_submit,
143
+ inputs=[msg, chatbot],
144
+ outputs=[msg, chatbot],
145
+ queue=False,
146
+ ).then(
147
+ bot_respond,
148
+ inputs=[chatbot],
149
+ outputs=[chatbot],
150
+ )
151
+
152
+ send_btn.click(
153
+ user_submit,
154
+ inputs=[msg, chatbot],
155
+ outputs=[msg, chatbot],
156
+ queue=False,
157
+ ).then(
158
+ bot_respond,
159
+ inputs=[chatbot],
160
+ outputs=[chatbot],
161
+ )
162
+
163
+ clear_btn.click(
164
+ lambda: ([], ""),
165
+ outputs=[chatbot, msg],
166
+ queue=False,
167
+ )
168
+
169
+ if __name__ == "__main__":
170
+ demo.queue(max_size=5).launch()
171
+