Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ with open("./model.gguf", mode="wb") as file:
|
|
17 |
file.write(response.content)
|
18 |
print("Model downloaded")
|
19 |
|
20 |
-
#
|
21 |
command = ["python3", "-m", "llama_cpp.server", "--model", "./model.gguf", "--host", "0.0.0.0", "--port", "2600", "--n_threads", "2"]
|
22 |
server_process = subprocess.Popen(command) # Almacenamos el proceso para poder terminarlo más tarde
|
23 |
print("Model server starting...")
|
@@ -34,48 +34,45 @@ def response(message, history):
|
|
34 |
response_text = ""
|
35 |
|
36 |
try:
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
print("Error al decodificar JSON:", e)
|
55 |
break
|
56 |
-
elif text.strip():
|
57 |
-
print("Respuesta no JSON:", text)
|
58 |
-
break
|
59 |
except requests.exceptions.RequestException as e:
|
60 |
print(f"Error al realizar la solicitud: {e}")
|
61 |
|
62 |
yield response_text
|
63 |
|
64 |
-
# Asegurarse de finalizar el proceso del servidor al finalizar el uso
|
65 |
def cleanup_server():
|
66 |
print("Closing server...")
|
67 |
-
server_process.terminate() #
|
68 |
-
server_process.wait() #
|
69 |
print("Server closed.")
|
70 |
|
71 |
-
#
|
72 |
gr_interface = gr.ChatInterface(
|
73 |
fn=response,
|
74 |
-
title="Mistral-7B-Instruct-v0.2-GGUF
|
75 |
theme='syddharth/gray-minimal'
|
76 |
)
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
17 |
file.write(response.content)
|
18 |
print("Model downloaded")
|
19 |
|
20 |
+
# Ejecutar el servidor LLM
|
21 |
command = ["python3", "-m", "llama_cpp.server", "--model", "./model.gguf", "--host", "0.0.0.0", "--port", "2600", "--n_threads", "2"]
|
22 |
server_process = subprocess.Popen(command) # Almacenamos el proceso para poder terminarlo más tarde
|
23 |
print("Model server starting...")
|
|
|
34 |
response_text = ""
|
35 |
|
36 |
try:
|
37 |
+
# Eliminado el timeout para esperar indefinidamente
|
38 |
+
with requests.post(url, json=body, stream=True) as stream_response:
|
39 |
+
for text_chunk in stream_response.iter_content(chunk_size=None):
|
40 |
+
text = text_chunk.decode('utf-8')
|
41 |
+
print("Respuesta cruda:", text) # Imprimir la respuesta cruda para depuración
|
42 |
|
43 |
+
if text.startswith("data: "):
|
44 |
+
text = text.replace("data: ", "")
|
45 |
+
if text.startswith("{") and "choices" in text:
|
46 |
+
try:
|
47 |
+
response_json = json.loads(text)
|
48 |
+
part = response_json["choices"][0]["text"]
|
49 |
+
print(part, end="", flush=True)
|
50 |
+
response_text += part
|
51 |
+
except json.JSONDecodeError as e:
|
52 |
+
print("Error al decodificar JSON:", e)
|
53 |
+
break
|
54 |
+
elif text.strip():
|
55 |
+
print("Respuesta no JSON:", text)
|
|
|
56 |
break
|
|
|
|
|
|
|
57 |
except requests.exceptions.RequestException as e:
|
58 |
print(f"Error al realizar la solicitud: {e}")
|
59 |
|
60 |
yield response_text
|
61 |
|
|
|
62 |
def cleanup_server():
|
63 |
print("Closing server...")
|
64 |
+
server_process.terminate() # Terminar el proceso del servidor
|
65 |
+
server_process.wait() # Esperar a que el proceso termine
|
66 |
print("Server closed.")
|
67 |
|
68 |
+
# Configurar y lanzar la interfaz de Gradio
|
69 |
gr_interface = gr.ChatInterface(
|
70 |
fn=response,
|
71 |
+
title="Mistral-7B-Instruct-v0.2-GGUF Chatbot",
|
72 |
theme='syddharth/gray-minimal'
|
73 |
)
|
74 |
|
75 |
+
try:
|
76 |
+
gr_interface.launch(share=True)
|
77 |
+
finally:
|
78 |
+
cleanup_server() # Asegurarse de limpiar el servidor al finalizar
|