Update app.py
Browse files
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 =
|
| 10 |
|
| 11 |
print("Cargando modelo...")
|
| 12 |
|
|
@@ -23,141 +23,135 @@ model.eval()
|
|
| 23 |
print("Modelo cargado correctamente.")
|
| 24 |
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
| 27 |
@app.route("/")
|
| 28 |
def index():
|
| 29 |
return Response("""
|
| 30 |
<!DOCTYPE html>
|
| 31 |
-
<html
|
| 32 |
<head>
|
| 33 |
<meta charset="UTF-8">
|
| 34 |
-
<title>
|
| 35 |
<style>
|
| 36 |
body {
|
| 37 |
-
margin:
|
| 38 |
-
font-family:
|
| 39 |
-
background
|
| 40 |
-
display:
|
| 41 |
-
justify-content:
|
| 42 |
-
align-items:
|
| 43 |
-
height:
|
| 44 |
}
|
| 45 |
-
.
|
| 46 |
-
width:
|
| 47 |
-
height:
|
| 48 |
-
background
|
| 49 |
-
display:
|
| 50 |
-
flex-direction:
|
| 51 |
-
border-radius:
|
| 52 |
-
overflow: hidden;
|
| 53 |
}
|
| 54 |
-
.chat
|
| 55 |
-
flex:
|
| 56 |
-
padding:
|
| 57 |
-
overflow-y:
|
| 58 |
-
display:
|
| 59 |
-
flex-direction:
|
| 60 |
}
|
| 61 |
-
.
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
max-width:
|
| 66 |
-
white-space:
|
| 67 |
}
|
| 68 |
-
.user
|
| 69 |
-
background
|
| 70 |
-
align-self:
|
| 71 |
-
color:
|
| 72 |
}
|
| 73 |
-
.bot
|
| 74 |
-
background
|
| 75 |
-
align-self:
|
| 76 |
-
color:
|
| 77 |
}
|
| 78 |
-
.input-area
|
| 79 |
-
display:
|
| 80 |
-
border-top:
|
| 81 |
}
|
| 82 |
-
input
|
| 83 |
-
flex:
|
| 84 |
-
padding:
|
| 85 |
-
border:
|
| 86 |
-
outline:
|
| 87 |
-
font-size:
|
| 88 |
}
|
| 89 |
-
button
|
| 90 |
-
padding:
|
| 91 |
-
border:
|
| 92 |
-
background
|
| 93 |
-
|
| 94 |
-
|
| 95 |
}
|
| 96 |
-
button:hover
|
| 97 |
-
opacity:
|
| 98 |
}
|
| 99 |
</style>
|
| 100 |
</head>
|
| 101 |
<body>
|
| 102 |
|
| 103 |
-
<div class="
|
| 104 |
-
<div id="chat
|
| 105 |
<div class="input-area">
|
| 106 |
-
<input
|
| 107 |
-
<button onclick="
|
| 108 |
</div>
|
| 109 |
</div>
|
| 110 |
|
| 111 |
<script>
|
| 112 |
-
async function
|
| 113 |
-
const input = document.getElementById("
|
| 114 |
-
const
|
| 115 |
|
| 116 |
-
const
|
| 117 |
-
if
|
| 118 |
|
| 119 |
-
|
| 120 |
-
input.value
|
| 121 |
|
| 122 |
-
const loading =
|
| 123 |
|
| 124 |
-
try
|
| 125 |
-
const
|
| 126 |
-
method:
|
| 127 |
-
headers:
|
| 128 |
-
body:
|
| 129 |
});
|
| 130 |
|
| 131 |
-
const data = await
|
| 132 |
loading.remove();
|
|
|
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
} catch (error) {
|
| 137 |
loading.remove();
|
| 138 |
-
|
| 139 |
}
|
| 140 |
}
|
| 141 |
|
| 142 |
-
function
|
| 143 |
-
const
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
| 151 |
-
|
| 152 |
-
return messageDiv;
|
| 153 |
}
|
| 154 |
|
| 155 |
-
document.getElementById("
|
| 156 |
-
.
|
| 157 |
-
|
| 158 |
-
sendMessage();
|
| 159 |
-
}
|
| 160 |
-
});
|
| 161 |
</script>
|
| 162 |
|
| 163 |
</body>
|
|
@@ -165,7 +159,9 @@ document.getElementById("user-input")
|
|
| 165 |
""", mimetype="text/html")
|
| 166 |
|
| 167 |
|
| 168 |
-
#
|
|
|
|
|
|
|
| 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": "
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 188 |
-
top_p=0.
|
| 189 |
-
repetition_penalty=1.
|
| 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 |
)
|