carlosdimare commited on
Commit
21eb661
verified
1 Parent(s): 2965f38

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request
2
+ from transformers import AutoTokenizer
3
+ from transformers.adapters import AutoAdapterModel
4
+
5
+ app = Flask(__name__)
6
+
7
+ # Cargar el modelo y tokenizer
8
+ model_name = "carlosdimare/clascon" # Nombre del modelo en Hugging Face
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoAdapterModel.from_pretrained(model_name)
11
+
12
+ # Cargar el adapter preentrenado
13
+ model.load_adapter(model_name, set_active=True)
14
+
15
+ # Funci贸n para generar la respuesta
16
+ def generar_respuesta(prompt):
17
+ inputs = tokenizer(prompt, return_tensors="pt")
18
+ outputs = model.generate(inputs["input_ids"], max_length=100, num_beams=5, no_repeat_ngram_size=2, early_stopping=True)
19
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
20
+
21
+ # Ruta principal para el formulario de chat
22
+ @app.route("/", methods=["GET", "POST"])
23
+ def index():
24
+ response = None
25
+ if request.method == "POST":
26
+ prompt = request.form["prompt"]
27
+ response = generar_respuesta(prompt)
28
+ return render_template("index.html", response=response)
29
+
30
+ # Ruta para manejar el chat
31
+ @app.route("/chat", methods=["POST"])
32
+ def chat():
33
+ prompt = request.form["prompt"]
34
+ response = generar_respuesta(prompt)
35
+ return render_template("index.html", response=response)
36
+
37
+ if __name__ == "__main__":
38
+ app.run(debug=True)