Chompi10 commited on
Commit
b6a565f
·
verified ·
1 Parent(s): a918733

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -102
app.py CHANGED
@@ -6,7 +6,7 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
6
  app = Flask(__name__)
7
 
8
  MODEL_NAME = "microsoft/phi-2"
9
- MAX_NEW_TOKENS = 200
10
 
11
  print("Cargando modelo...")
12
 
@@ -23,141 +23,135 @@ model.eval()
23
  print("Modelo cargado correctamente.")
24
 
25
 
26
- # ---------- FRONTEND ----------
 
 
27
  @app.route("/")
28
  def index():
29
  return Response("""
30
  <!DOCTYPE html>
31
- <html lang="es">
32
  <head>
33
  <meta charset="UTF-8">
34
- <title>Asistente IA</title>
35
  <style>
36
  body {
37
- margin: 0;
38
- font-family: Arial, sans-serif;
39
- background-color: #343541;
40
- display: flex;
41
- justify-content: center;
42
- align-items: center;
43
- height: 100vh;
44
  }
45
- .chat-container {
46
- width: 700px;
47
- height: 85vh;
48
- background-color: #444654;
49
- display: flex;
50
- flex-direction: column;
51
- border-radius: 10px;
52
- overflow: hidden;
53
  }
54
- .chat-box {
55
- flex: 1;
56
- padding: 20px;
57
- overflow-y: auto;
58
- display: flex;
59
- flex-direction: column;
60
  }
61
- .message {
62
- margin-bottom: 15px;
63
- padding: 12px;
64
- border-radius: 8px;
65
- max-width: 80%;
66
- white-space: pre-wrap;
67
  }
68
- .user {
69
- background-color: #19c37d;
70
- align-self: flex-end;
71
- color: black;
72
  }
73
- .bot {
74
- background-color: #555869;
75
- align-self: flex-start;
76
- color: white;
77
  }
78
- .input-area {
79
- display: flex;
80
- border-top: 1px solid #555;
81
  }
82
- input {
83
- flex: 1;
84
- padding: 15px;
85
- border: none;
86
- outline: none;
87
- font-size: 16px;
88
  }
89
- button {
90
- padding: 15px;
91
- border: none;
92
- background-color: #19c37d;
93
- cursor: pointer;
94
- font-weight: bold;
95
  }
96
- button:hover {
97
- opacity: 0.9;
98
  }
99
  </style>
100
  </head>
101
  <body>
102
 
103
- <div class="chat-container">
104
- <div id="chat-box" class="chat-box"></div>
105
  <div class="input-area">
106
- <input type="text" id="user-input" placeholder="Escribí tu mensaje..." />
107
- <button onclick="sendMessage()">Enviar</button>
108
  </div>
109
  </div>
110
 
111
  <script>
112
- async function sendMessage() {
113
- const input = document.getElementById("user-input");
114
- const chatBox = document.getElementById("chat-box");
115
 
116
- const userMessage = input.value.trim();
117
- if (!userMessage) return;
118
 
119
- addMessage(userMessage, "user");
120
- input.value = "";
121
 
122
- const loading = addMessage("Escribiendo...", "bot");
123
 
124
- try {
125
- const response = await fetch("/chat", {
126
- method: "POST",
127
- headers: { "Content-Type": "application/json" },
128
- body: JSON.stringify({ message: userMessage })
129
  });
130
 
131
- const data = await response.json();
132
  loading.remove();
 
133
 
134
- addMessage(data.response || "Error en la respuesta", "bot");
135
-
136
- } catch (error) {
137
  loading.remove();
138
- addMessage("Error conectando con el servidor", "bot");
139
  }
140
  }
141
 
142
- function addMessage(text, sender) {
143
- const chatBox = document.getElementById("chat-box");
144
-
145
- const messageDiv = document.createElement("div");
146
- messageDiv.classList.add("message", sender);
147
- messageDiv.innerText = text;
148
-
149
- chatBox.appendChild(messageDiv);
150
- chatBox.scrollTop = chatBox.scrollHeight;
151
-
152
- return messageDiv;
153
  }
154
 
155
- document.getElementById("user-input")
156
- .addEventListener("keypress", function(e) {
157
- if (e.key === "Enter") {
158
- sendMessage();
159
- }
160
- });
161
  </script>
162
 
163
  </body>
@@ -165,7 +159,9 @@ document.getElementById("user-input")
165
  """, mimetype="text/html")
166
 
167
 
168
- # ---------- API ----------
 
 
169
  @app.route("/chat", methods=["POST"])
170
  def chat():
171
  try:
@@ -173,9 +169,22 @@ def chat():
173
  user_input = data.get("message", "")
174
 
175
  if not user_input:
176
- return jsonify({"error": "Mensaje vacío"}), 400
177
-
178
- prompt = f"User: {user_input}\nAssistant:"
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
  inputs = tokenizer(prompt, return_tensors="pt")
181
 
@@ -184,9 +193,9 @@ def chat():
184
  **inputs,
185
  max_new_tokens=MAX_NEW_TOKENS,
186
  do_sample=True,
187
- temperature=0.7,
188
- top_p=0.9,
189
- repetition_penalty=1.2,
190
  no_repeat_ngram_size=3,
191
  pad_token_id=tokenizer.eos_token_id
192
  )
 
6
  app = Flask(__name__)
7
 
8
  MODEL_NAME = "microsoft/phi-2"
9
+ MAX_NEW_TOKENS = 400
10
 
11
  print("Cargando modelo...")
12
 
 
23
  print("Modelo cargado correctamente.")
24
 
25
 
26
+ # ===============================
27
+ # FRONTEND
28
+ # ===============================
29
  @app.route("/")
30
  def index():
31
  return Response("""
32
  <!DOCTYPE html>
33
+ <html>
34
  <head>
35
  <meta charset="UTF-8">
36
+ <title>AI Assistant</title>
37
  <style>
38
  body {
39
+ margin:0;
40
+ font-family:Arial;
41
+ background:#343541;
42
+ display:flex;
43
+ justify-content:center;
44
+ align-items:center;
45
+ height:100vh;
46
  }
47
+ .container{
48
+ width:800px;
49
+ height:90vh;
50
+ background:#444654;
51
+ display:flex;
52
+ flex-direction:column;
53
+ border-radius:10px;
 
54
  }
55
+ .chat{
56
+ flex:1;
57
+ padding:20px;
58
+ overflow-y:auto;
59
+ display:flex;
60
+ flex-direction:column;
61
  }
62
+ .msg{
63
+ padding:12px;
64
+ border-radius:8px;
65
+ margin-bottom:12px;
66
+ max-width:85%;
67
+ white-space:pre-wrap;
68
  }
69
+ .user{
70
+ background:#19c37d;
71
+ align-self:flex-end;
72
+ color:black;
73
  }
74
+ .bot{
75
+ background:#555869;
76
+ align-self:flex-start;
77
+ color:white;
78
  }
79
+ .input-area{
80
+ display:flex;
81
+ border-top:1px solid #555;
82
  }
83
+ input{
84
+ flex:1;
85
+ padding:15px;
86
+ border:none;
87
+ outline:none;
88
+ font-size:16px;
89
  }
90
+ button{
91
+ padding:15px;
92
+ border:none;
93
+ background:#19c37d;
94
+ font-weight:bold;
95
+ cursor:pointer;
96
  }
97
+ button:hover{
98
+ opacity:0.9;
99
  }
100
  </style>
101
  </head>
102
  <body>
103
 
104
+ <div class="container">
105
+ <div id="chat" class="chat"></div>
106
  <div class="input-area">
107
+ <input id="input" placeholder="Write your message..." />
108
+ <button onclick="send()">Send</button>
109
  </div>
110
  </div>
111
 
112
  <script>
113
+ async function send(){
114
+ const input = document.getElementById("input");
115
+ const chat = document.getElementById("chat");
116
 
117
+ const text = input.value.trim();
118
+ if(!text) return;
119
 
120
+ add(text,"user");
121
+ input.value="";
122
 
123
+ const loading = add("Thinking...","bot");
124
 
125
+ try{
126
+ const res = await fetch("/chat",{
127
+ method:"POST",
128
+ headers:{"Content-Type":"application/json"},
129
+ body:JSON.stringify({message:text})
130
  });
131
 
132
+ const data = await res.json();
133
  loading.remove();
134
+ add(data.response || "Error","bot");
135
 
136
+ }catch(e){
 
 
137
  loading.remove();
138
+ add("Server error","bot");
139
  }
140
  }
141
 
142
+ function add(text,type){
143
+ const chat=document.getElementById("chat");
144
+ const div=document.createElement("div");
145
+ div.className="msg "+type;
146
+ div.innerText=text;
147
+ chat.appendChild(div);
148
+ chat.scrollTop=chat.scrollHeight;
149
+ return div;
 
 
 
150
  }
151
 
152
+ document.getElementById("input").addEventListener("keypress",function(e){
153
+ if(e.key==="Enter"){ send(); }
154
+ });
 
 
 
155
  </script>
156
 
157
  </body>
 
159
  """, mimetype="text/html")
160
 
161
 
162
+ # ===============================
163
+ # API CHAT
164
+ # ===============================
165
  @app.route("/chat", methods=["POST"])
166
  def chat():
167
  try:
 
169
  user_input = data.get("message", "")
170
 
171
  if not user_input:
172
+ return jsonify({"error": "Empty message"}), 400
173
+
174
+ # PROMPT MULTI-LENGUAJE + CÓDIGO COMPLETO
175
+ system_prompt = """
176
+ You are an advanced AI assistant.
177
+ Always reply in the SAME language as the user.
178
+ If the user asks for code:
179
+ - Provide COMPLETE working code.
180
+ - Include all imports.
181
+ - Do not cut the code.
182
+ - Use proper formatting.
183
+ - Use markdown triple backticks for code blocks.
184
+ Be clear and professional.
185
+ """
186
+
187
+ prompt = f"{system_prompt}\nUser: {user_input}\nAssistant:"
188
 
189
  inputs = tokenizer(prompt, return_tensors="pt")
190
 
 
193
  **inputs,
194
  max_new_tokens=MAX_NEW_TOKENS,
195
  do_sample=True,
196
+ temperature=0.2,
197
+ top_p=0.7,
198
+ repetition_penalty=1.1,
199
  no_repeat_ngram_size=3,
200
  pad_token_id=tokenizer.eos_token_id
201
  )