Kukedlc commited on
Commit
06e12a0
1 Parent(s): 803a159

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -30
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
- # Iniciar el servidor LLM y almacenar el proceso
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
- stream_response = requests.post(url, json=body, stream=True, timeout=60)
38
- for text_chunk in stream_response.iter_content(chunk_size=None):
39
- text = text_chunk.decode('utf-8')
 
 
40
 
41
- # Imprimir la respuesta cruda para depuración
42
- print("Respuesta cruda:", text)
43
-
44
- if text.startswith("data: "):
45
- text = text.replace("data: ", "")
46
-
47
- if text.startswith("{") and "choices" in text:
48
- try:
49
- response_json = json.loads(text)
50
- part = response_json["choices"][0]["text"]
51
- print(part, end="", flush=True)
52
- response_text += part
53
- except json.JSONDecodeError as e:
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() # Envía la señal para terminar el proceso
68
- server_process.wait() # Espera a que el proceso termine
69
  print("Server closed.")
70
 
71
- # Configuración de la interfaz de Gradio
72
  gr_interface = gr.ChatInterface(
73
  fn=response,
74
- title="Mistral-7B-Instruct-v0.2-GGUF Eugenio Schiavoni Chatbot",
75
  theme='syddharth/gray-minimal'
76
  )
77
 
78
- # Añadir un paso de limpieza antes de cerrar la aplicación
79
- gr.Interface.cleanup = cleanup_server
80
-
81
- gr_interface.launch(share=True)
 
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