Update modules/chatbot.py
Browse files- modules/chatbot.py +15 -11
modules/chatbot.py
CHANGED
|
@@ -1,18 +1,22 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class Llama2Chatbot:
|
| 5 |
def __init__(self):
|
| 6 |
-
self.
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
def generate_response(self, prompt
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
response
|
| 15 |
-
return response
|
| 16 |
|
| 17 |
def initialize_chatbot():
|
| 18 |
return Llama2Chatbot()
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
# Cargar variables de entorno
|
| 6 |
+
load_dotenv()
|
| 7 |
|
| 8 |
class Llama2Chatbot:
|
| 9 |
def __init__(self):
|
| 10 |
+
self.API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-hf"
|
| 11 |
+
api_key = os.getenv("HF_API_KEY")
|
| 12 |
+
if not api_key:
|
| 13 |
+
raise ValueError("No se encontr贸 la clave de API de Hugging Face. Aseg煤rate de configurar la variable de entorno HF_API_KEY.")
|
| 14 |
+
self.headers = {"Authorization": f"Bearer {api_key}"}
|
| 15 |
|
| 16 |
+
def generate_response(self, prompt):
|
| 17 |
+
payload = {"inputs": prompt}
|
| 18 |
+
response = requests.post(self.API_URL, headers=self.headers, json=payload)
|
| 19 |
+
return response.json()[0]['generated_text']
|
|
|
|
| 20 |
|
| 21 |
def initialize_chatbot():
|
| 22 |
return Llama2Chatbot()
|