salomonsky commited on
Commit
99da48b
verified
1 Parent(s): 425d532

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -27
app.py CHANGED
@@ -1,15 +1,28 @@
1
- from functools import lru_cache
2
- import gradio as gr
3
- import threading
4
- import time
5
  from huggingface_hub import InferenceClient
 
6
 
7
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
8
  system_prompt = "Deber谩s proporcinar c贸digo limpio, resumido, investigando cada l铆nea en internet en foros, revisa stackoverflow.com para consultas, elimina comentarios siempre, concatena el funcionamiento de los bloques y bibliotecas, y esquematiza el funcionamiento global del c贸digo. Preve茅 posibles errores y complementa al final con una tabla explicando el funcionamiento, propon alternativas de bibliotecas para solucionar errores, siempre consulta internet para posibles resoluciones."
9
  system_prompt_sent = False
10
 
11
- @lru_cache(maxsize=128)
12
- def generate_text(prompt, temperature=0.9, max_new_tokens=4096, top_p=0.95, repetition_penalty=1.0):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  global system_prompt_sent
14
  temperature = float(temperature)
15
  if temperature < 1e-2:
@@ -25,35 +38,17 @@ def generate_text(prompt, temperature=0.9, max_new_tokens=4096, top_p=0.95, repe
25
  seed=42,
26
  )
27
 
28
- formatted_prompt = format_prompt(prompt, system_prompt_sent)
29
-
30
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
31
  output = ""
32
 
33
  for response in stream:
34
  output += response.token.text
35
-
36
- system_prompt_sent = False
37
 
38
  return output
39
 
40
- def format_prompt(message, system_prompt_sent):
41
- prompt = " "
42
-
43
- if not system_prompt_sent:
44
- prompt += f"[INST] {system_prompt} [/ "
45
- system_prompt_sent = True
46
-
47
- return prompt
48
-
49
- def generate(
50
- message, history, temperature=0.9, max_new_tokens=4096, top_p=0.95, repetition_penalty=1.0,
51
- ):
52
- t = threading.Thread(target=generate_text, args=(message, temperature, max_new_tokens, top_p, repetition_penalty))
53
- t.start()
54
- t.join()
55
- return generate_text.cache_info()[1], generate_text(message, temperature, max_new_tokens, top_p, repetition_penalty)
56
-
57
  chat_interface = gr.ChatInterface(
58
  fn=generate,
59
  chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=False, likeable=False, layout="vertical", height=900),
 
 
 
 
 
1
  from huggingface_hub import InferenceClient
2
+ import gradio as gr
3
 
4
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
5
  system_prompt = "Deber谩s proporcinar c贸digo limpio, resumido, investigando cada l铆nea en internet en foros, revisa stackoverflow.com para consultas, elimina comentarios siempre, concatena el funcionamiento de los bloques y bibliotecas, y esquematiza el funcionamiento global del c贸digo. Preve茅 posibles errores y complementa al final con una tabla explicando el funcionamiento, propon alternativas de bibliotecas para solucionar errores, siempre consulta internet para posibles resoluciones."
6
  system_prompt_sent = False
7
 
8
+ def format_prompt(message, history):
9
+ global system_prompt_sent
10
+ prompt = "<s>"
11
+
12
+ if not any(f"[INST] {system_prompt} [/INST]" in user_prompt for user_prompt, _ in history):
13
+ prompt += f"[INST] {system_prompt} [/INST]"
14
+ system_prompt_sent = True
15
+
16
+ for user_prompt, bot_response in history:
17
+ prompt += f"[INST] {user_prompt} [/INST]"
18
+ prompt += f" {bot_response}</s> "
19
+
20
+ prompt += f"[INST] {message} [/INST]"
21
+ return prompt
22
+
23
+ def generate(
24
+ prompt, history, temperature=0.9, max_new_tokens=4096, top_p=0.95, repetition_penalty=1.0,
25
+ ):
26
  global system_prompt_sent
27
  temperature = float(temperature)
28
  if temperature < 1e-2:
 
38
  seed=42,
39
  )
40
 
41
+ formatted_prompt = format_prompt(prompt, history)
42
+
43
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
44
  output = ""
45
 
46
  for response in stream:
47
  output += response.token.text
48
+ yield output
 
49
 
50
  return output
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  chat_interface = gr.ChatInterface(
53
  fn=generate,
54
  chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=False, likeable=False, layout="vertical", height=900),