Spaces:
Runtime error
Runtime error
Create model_manager.py
Browse files- model_manager.py +71 -0
model_manager.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# model_manager.py
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import asyncio
|
| 4 |
+
|
| 5 |
+
class ModelManager:
|
| 6 |
+
def __init__(self, cache_dir="./model_cache"):
|
| 7 |
+
self.models = {}
|
| 8 |
+
self.models_loaded = False
|
| 9 |
+
self.cache_dir = cache_dir
|
| 10 |
+
|
| 11 |
+
async def load_models_async(self):
|
| 12 |
+
async def load(key, model_name):
|
| 13 |
+
try:
|
| 14 |
+
self.models[key] = pipeline(
|
| 15 |
+
"text-generation",
|
| 16 |
+
model=model_name,
|
| 17 |
+
device="cpu",
|
| 18 |
+
model_kwargs={"cache_dir": self.cache_dir}
|
| 19 |
+
)
|
| 20 |
+
except Exception as e:
|
| 21 |
+
self.models[key] = None
|
| 22 |
+
tasks = [
|
| 23 |
+
load("chat","microsoft/DialoGPT-small"),
|
| 24 |
+
load("code","microsoft/CodeGPT-small-py"),
|
| 25 |
+
load("creative","microsoft/DialoGPT-small"),
|
| 26 |
+
]
|
| 27 |
+
await asyncio.gather(*tasks)
|
| 28 |
+
self.models_loaded = True
|
| 29 |
+
|
| 30 |
+
def get_model_for_task(self, task_type):
|
| 31 |
+
mapping = {
|
| 32 |
+
"CONVERSATION": self.models.get("chat"),
|
| 33 |
+
"CODE_GENERATION": self.models.get("code"),
|
| 34 |
+
"CREATIVE_WRITING": self.models.get("creative"),
|
| 35 |
+
}
|
| 36 |
+
return mapping.get(task_type, self.models.get("chat"))
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# api_agent.py
|
| 40 |
+
import requests
|
| 41 |
+
|
| 42 |
+
class APIAgent:
|
| 43 |
+
def __init__(self, config):
|
| 44 |
+
self.config = config # debe contener keys ya cargadas desde /config
|
| 45 |
+
self.session = requests.Session()
|
| 46 |
+
|
| 47 |
+
def call_openai(self, prompt, system_message=""):
|
| 48 |
+
key = self.config.get("openai_api_key", "")
|
| 49 |
+
if not key:
|
| 50 |
+
return None
|
| 51 |
+
url = "https://api.openai.com/v1/chat/completions"
|
| 52 |
+
headers = {"Authorization": f"Bearer {key}", "Content-Type": "application/json"}
|
| 53 |
+
payload = {"model":"gpt-3.5-turbo", "messages":[{"role":"system","content":system_message},{"role":"user","content":prompt}], "temperature": self.config.get("temperature", 0.7), "max_tokens": self.config.get("max_tokens", 600)}
|
| 54 |
+
r = self.session.post(url, headers=headers, json=payload, timeout=self.config.get("timeout", 30))
|
| 55 |
+
if r.ok:
|
| 56 |
+
return r.json()["choices"][0]["message"]["content"]
|
| 57 |
+
return None
|
| 58 |
+
|
| 59 |
+
def call_deepseek(self, prompt, system_message=""):
|
| 60 |
+
key = self.config.get("deepseek_api_key", "")
|
| 61 |
+
if not key:
|
| 62 |
+
return None
|
| 63 |
+
# similar a OpenAI: payload adaptado
|
| 64 |
+
url = "https://api.deepseek.com/v1/chat/completions"
|
| 65 |
+
headers = {"Authorization": f"Bearer {key}", "Content-Type": "application/json"}
|
| 66 |
+
payload = {"model":"deepseek-chat","messages":[{"role":"system","content":system_message},{"role":"user","content":prompt}]}
|
| 67 |
+
r = self.session.post(url, headers=headers, json=payload, timeout=self.config.get("timeout", 30))
|
| 68 |
+
if r.ok:
|
| 69 |
+
return r.json()["choices"][0]["message"]["content"]
|
| 70 |
+
return None
|
| 71 |
+
|