Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -23,15 +23,26 @@ from typing_extensions import TypedDict
|
|
23 |
# Configuraci贸n de Ollama y su servicio
|
24 |
OLLAMA = os.path.expanduser("~/ollama")
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
try:
|
28 |
if not os.path.exists(OLLAMA):
|
29 |
print("Ollama no encontrado, descargando...")
|
30 |
-
subprocess.run("curl -
|
31 |
os.chmod(OLLAMA, 0o755)
|
32 |
|
33 |
print(f"Descargando el modelo: {model_name}")
|
34 |
-
subprocess.run(["ollama", "pull", model_name], check=True)
|
35 |
except subprocess.CalledProcessError as e:
|
36 |
print(f"Error al descargar el modelo: {e}")
|
37 |
raise
|
@@ -40,7 +51,7 @@ def download_ollama_model(model_name='hf.co/MaziyarPanahi/Llama-3.2-3B-Instruct-
|
|
40 |
async def async_download_ollama_model():
|
41 |
await asyncio.to_thread(download_ollama_model)
|
42 |
|
43 |
-
# Iniciar el
|
44 |
def ollama_service_thread():
|
45 |
print("Iniciando el servicio de Ollama")
|
46 |
subprocess.run("~/ollama serve", shell=True)
|
@@ -134,25 +145,25 @@ class GraphState(TypedDict):
|
|
134 |
context: str
|
135 |
|
136 |
# Nodos de procesamiento
|
137 |
-
def generate(state):
|
138 |
print("Step: Generating Final Response")
|
139 |
question = state["question"]
|
140 |
context = state["context"]
|
141 |
-
generation = generate_chain.invoke
|
142 |
return {"generation": generation}
|
143 |
|
144 |
-
def transform_query(state):
|
145 |
print("Step: Optimizing Query for Web Search")
|
146 |
question = state['question']
|
147 |
-
gen_query = query_chain.invoke
|
148 |
search_query = gen_query.get("query", "") # Asegurarnos de que estamos obteniendo la clave correcta
|
149 |
return {"search_query": search_query}
|
150 |
|
151 |
-
def web_search(state):
|
152 |
search_query = state['search_query']
|
153 |
print(f'Step: Searching the Web for: "{search_query}"')
|
154 |
try:
|
155 |
-
search_result = web_search_tool.invoke
|
156 |
if isinstance(search_result, str): # Si la respuesta es una cadena, la convertimos en un diccionario
|
157 |
print(f"Respuesta de b煤squeda web es cadena: {search_result}")
|
158 |
return {"context": search_result}
|
@@ -164,10 +175,10 @@ def web_search(state):
|
|
164 |
print(f"Web search failed: {e}")
|
165 |
return None # Si la b煤squeda falla, no devuelve contexto
|
166 |
|
167 |
-
def route_question(state):
|
168 |
print("Step: Routing Query")
|
169 |
question = state['question']
|
170 |
-
output = question_router.invoke
|
171 |
if output.get('choice') == "web_search":
|
172 |
print("Step: Routing Query to Web Search")
|
173 |
return "websearch"
|
@@ -196,18 +207,15 @@ workflow.add_edge("generate", END)
|
|
196 |
local_agent = workflow.compile()
|
197 |
|
198 |
# Funci贸n para ejecutar el agente
|
199 |
-
def run_agent_parallel(query):
|
200 |
-
output = local_agent.invoke
|
201 |
-
if "generation" not in output: # Si la b煤squeda web fall贸 y no hubo respuesta de generaci贸n
|
202 |
-
print("Web search failed, using Ollama model directly.")
|
203 |
-
return generate({"question": query, "context": ""})["generation"] # Generar directamente
|
204 |
return output['generation']
|
205 |
|
206 |
# L贸gica del servidor FastAPI
|
207 |
@app.post("/query")
|
208 |
async def query_endpoint(request: QueryRequest):
|
209 |
query = request.query
|
210 |
-
return {"response": run_agent_parallel(query)}
|
211 |
|
212 |
# L贸gica de recursos
|
213 |
def release_resources():
|
@@ -249,4 +257,4 @@ def resource_manager():
|
|
249 |
resource_manager()
|
250 |
|
251 |
if __name__ == "__main__":
|
252 |
-
uvicorn.run(app, host="0.0.0.0", port=
|
|
|
23 |
# Configuraci贸n de Ollama y su servicio
|
24 |
OLLAMA = os.path.expanduser("~/ollama")
|
25 |
|
26 |
+
# Funci贸n para instalar Ollama
|
27 |
+
def install_ollama():
|
28 |
+
try:
|
29 |
+
print("Instalando Ollama...")
|
30 |
+
subprocess.run("curl -fsSL https://ollama.com/install.sh | sh", shell=True, check=True)
|
31 |
+
print("Ollama instalado con 茅xito.")
|
32 |
+
except subprocess.CalledProcessError as e:
|
33 |
+
print(f"Error al instalar Ollama: {e}")
|
34 |
+
raise
|
35 |
+
|
36 |
+
# Funci贸n para descargar el modelo de Ollama
|
37 |
+
async def download_ollama_model(model_name='hf.co/MaziyarPanahi/Llama-3.2-3B-Instruct-uncensored-GGUF:IQ1_S'):
|
38 |
try:
|
39 |
if not os.path.exists(OLLAMA):
|
40 |
print("Ollama no encontrado, descargando...")
|
41 |
+
subprocess.run("curl -L https://ollama.com/download/ollama-linux-amd64 -o ~/ollama", shell=True)
|
42 |
os.chmod(OLLAMA, 0o755)
|
43 |
|
44 |
print(f"Descargando el modelo: {model_name}")
|
45 |
+
subprocess.run(["~/ollama", "pull", model_name], check=True)
|
46 |
except subprocess.CalledProcessError as e:
|
47 |
print(f"Error al descargar el modelo: {e}")
|
48 |
raise
|
|
|
51 |
async def async_download_ollama_model():
|
52 |
await asyncio.to_thread(download_ollama_model)
|
53 |
|
54 |
+
# Iniciar el servicio de Ollama en un hilo
|
55 |
def ollama_service_thread():
|
56 |
print("Iniciando el servicio de Ollama")
|
57 |
subprocess.run("~/ollama serve", shell=True)
|
|
|
145 |
context: str
|
146 |
|
147 |
# Nodos de procesamiento
|
148 |
+
async def generate(state):
|
149 |
print("Step: Generating Final Response")
|
150 |
question = state["question"]
|
151 |
context = state["context"]
|
152 |
+
generation = await asyncio.to_thread(generate_chain.invoke, {"context": context, "question": question})
|
153 |
return {"generation": generation}
|
154 |
|
155 |
+
async def transform_query(state):
|
156 |
print("Step: Optimizing Query for Web Search")
|
157 |
question = state['question']
|
158 |
+
gen_query = await asyncio.to_thread(query_chain.invoke, {"question": question})
|
159 |
search_query = gen_query.get("query", "") # Asegurarnos de que estamos obteniendo la clave correcta
|
160 |
return {"search_query": search_query}
|
161 |
|
162 |
+
async def web_search(state):
|
163 |
search_query = state['search_query']
|
164 |
print(f'Step: Searching the Web for: "{search_query}"')
|
165 |
try:
|
166 |
+
search_result = await asyncio.to_thread(web_search_tool.invoke, search_query)
|
167 |
if isinstance(search_result, str): # Si la respuesta es una cadena, la convertimos en un diccionario
|
168 |
print(f"Respuesta de b煤squeda web es cadena: {search_result}")
|
169 |
return {"context": search_result}
|
|
|
175 |
print(f"Web search failed: {e}")
|
176 |
return None # Si la b煤squeda falla, no devuelve contexto
|
177 |
|
178 |
+
async def route_question(state):
|
179 |
print("Step: Routing Query")
|
180 |
question = state['question']
|
181 |
+
output = await asyncio.to_thread(question_router.invoke, {"question": question})
|
182 |
if output.get('choice') == "web_search":
|
183 |
print("Step: Routing Query to Web Search")
|
184 |
return "websearch"
|
|
|
207 |
local_agent = workflow.compile()
|
208 |
|
209 |
# Funci贸n para ejecutar el agente
|
210 |
+
async def run_agent_parallel(query):
|
211 |
+
output = await asyncio.to_thread(local_agent.invoke, {"question": query})
|
|
|
|
|
|
|
212 |
return output['generation']
|
213 |
|
214 |
# L贸gica del servidor FastAPI
|
215 |
@app.post("/query")
|
216 |
async def query_endpoint(request: QueryRequest):
|
217 |
query = request.query
|
218 |
+
return {"response": await run_agent_parallel(query)}
|
219 |
|
220 |
# L贸gica de recursos
|
221 |
def release_resources():
|
|
|
257 |
resource_manager()
|
258 |
|
259 |
if __name__ == "__main__":
|
260 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|