Mattral commited on
Commit
aeb7f98
1 Parent(s): 353e3ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -18,31 +18,36 @@ with open("info2.md", "r") as file:
18
  info2_md_content = file.read()
19
 
20
  # Chunk the info.md and info2.md content into smaller sections
21
- chunk_size = 2500 # Adjust this size as needed
22
  info_md_chunks = textwrap.wrap(info_md_content, chunk_size)
23
  info2_md_chunks = textwrap.wrap(info2_md_content, chunk_size)
24
 
25
  # Combine both sets of chunks
26
  all_chunks = info_md_chunks + info2_md_chunks
27
 
 
 
 
 
 
 
 
 
 
 
28
  def format_prompt_mixtral(message, history, chunks):
29
  prompt = "<s>"
30
  prompt += f"{system_prompt_text}\n\n" # Add the system prompt
31
 
32
  # Include the initial context from the chunks
33
- for chunk in chunks:
34
- prompt += f"[INST] System Information [/INST] {chunk}</s> "
35
 
36
- # Add conversation history
37
- if history:
38
- for user_prompt, bot_response in history:
39
- prompt += f"[INST] {user_prompt} [/INST] {bot_response}</s> "
40
-
41
  # Add the current user message
42
  prompt += f"[INST] {message} [/INST]"
43
  return prompt
44
 
45
- def chat_inf(message, history, seed, temp, tokens, top_p, rep_p):
46
  generate_kwargs = dict(
47
  temperature=temp,
48
  max_new_tokens=tokens,
@@ -52,7 +57,7 @@ def chat_inf(message, history, seed, temp, tokens, top_p, rep_p):
52
  seed=seed,
53
  )
54
 
55
- formatted_prompt = format_prompt_mixtral(message, history, all_chunks)
56
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
57
  output = ""
58
  for response in stream:
@@ -62,6 +67,8 @@ def chat_inf(message, history, seed, temp, tokens, top_p, rep_p):
62
  yield history
63
 
64
  def clear_fn():
 
 
65
  return None, None
66
 
67
  rand_val = random.randint(1, 1111111111111111)
@@ -72,7 +79,7 @@ def check_rand(inp, val):
72
  else:
73
  return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=int(val))
74
 
75
- with gr.Blocks() as app: # Add auth here
76
  gr.HTML("""<center><h1 style='font-size:xx-large;'>PTT Chatbot</h1><br><h3>running on Huggingface Inference </h3><br><h7>EXPERIMENTAL</center>""")
77
  with gr.Row():
78
  chat = gr.Chatbot(height=500)
@@ -98,7 +105,7 @@ with gr.Blocks() as app: # Add auth here
98
 
99
  hid1 = gr.Number(value=1, visible=False)
100
 
101
- go = btn.click(check_rand, [rand, seed], seed).then(chat_inf, [inp, chat, seed, temp, tokens, top_p, rep_p], chat)
102
 
103
  stop_btn.click(None, None, None, cancels=[go])
104
  clear_btn.click(clear_fn, None, [inp, chat])
 
18
  info2_md_content = file.read()
19
 
20
  # Chunk the info.md and info2.md content into smaller sections
21
+ chunk_size = 1500 # Adjust this size as needed to fit the context window
22
  info_md_chunks = textwrap.wrap(info_md_content, chunk_size)
23
  info2_md_chunks = textwrap.wrap(info2_md_content, chunk_size)
24
 
25
  # Combine both sets of chunks
26
  all_chunks = info_md_chunks + info2_md_chunks
27
 
28
+ # Function to initialize the conversation history with chunks
29
+ def initialize_history(chunks):
30
+ history = []
31
+ for chunk in chunks:
32
+ history.append(("System Information", chunk))
33
+ return history
34
+
35
+ # Initialize history with initial chunks
36
+ history = initialize_history(all_chunks[:2]) # Starting with the first two chunks for example
37
+
38
  def format_prompt_mixtral(message, history, chunks):
39
  prompt = "<s>"
40
  prompt += f"{system_prompt_text}\n\n" # Add the system prompt
41
 
42
  # Include the initial context from the chunks
43
+ for user_prompt, bot_response in history:
44
+ prompt += f"[INST] {user_prompt} [/INST] {bot_response}</s> "
45
 
 
 
 
 
 
46
  # Add the current user message
47
  prompt += f"[INST] {message} [/INST]"
48
  return prompt
49
 
50
+ def chat_inf(message, history, chunks, seed, temp, tokens, top_p, rep_p):
51
  generate_kwargs = dict(
52
  temperature=temp,
53
  max_new_tokens=tokens,
 
57
  seed=seed,
58
  )
59
 
60
+ formatted_prompt = format_prompt_mixtral(message, history, chunks)
61
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
62
  output = ""
63
  for response in stream:
 
67
  yield history
68
 
69
  def clear_fn():
70
+ global history
71
+ history = initialize_history(all_chunks[:2]) # Reset to initial chunks
72
  return None, None
73
 
74
  rand_val = random.randint(1, 1111111111111111)
 
79
  else:
80
  return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=int(val))
81
 
82
+ with gr.Blocks() as app:
83
  gr.HTML("""<center><h1 style='font-size:xx-large;'>PTT Chatbot</h1><br><h3>running on Huggingface Inference </h3><br><h7>EXPERIMENTAL</center>""")
84
  with gr.Row():
85
  chat = gr.Chatbot(height=500)
 
105
 
106
  hid1 = gr.Number(value=1, visible=False)
107
 
108
+ go = btn.click(check_rand, [rand, seed], seed).then(chat_inf, [inp, chat, all_chunks, seed, temp, tokens, top_p, rep_p], chat)
109
 
110
  stop_btn.click(None, None, None, cancels=[go])
111
  clear_btn.click(clear_fn, None, [inp, chat])