Mattral commited on
Commit
c82cfaa
1 Parent(s): 31dc28b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -43
app.py CHANGED
@@ -10,44 +10,31 @@ client = InferenceClient(model)
10
  # Embedded system prompt
11
  system_prompt_text = "You are a smart and helpful co-worker of Thailand based multi-national company PTT, and PTTEP. You help with any kind of request and provide a detailed answer to the question. But if you are asked about something unethical or dangerous, you must refuse and provide a safe and respectful way to handle that."
12
 
13
- # Read the content of the info.md and info2.md files
14
  with open("info.md", "r") as file:
15
  info_md_content = file.read()
16
 
17
- 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 = 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):
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, seed, temp, tokens, top_p, rep_p):
51
  generate_kwargs = dict(
52
  temperature=temp,
53
  max_new_tokens=tokens,
@@ -57,29 +44,27 @@ def chat_inf(message, history, seed, temp, tokens, top_p, rep_p):
57
  seed=seed,
58
  )
59
 
60
- formatted_prompt = format_prompt_mixtral(message, history)
61
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
62
  output = ""
63
  for response in stream:
64
  output += response.token.text
65
- yield [(message, output)]
66
- history.append((message, output))
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)
75
 
76
- def check_rand(rand, val):
77
- if rand:
78
- return random.randint(1, 1111111111111111)
79
  else:
80
- return 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,13 +90,7 @@ with gr.Blocks() as app:
105
 
106
  hid1 = gr.Number(value=1, visible=False)
107
 
108
- def on_chat(message, chat, seed, temp, tokens, top_p, rep_p):
109
- chat.clear()
110
- history = initialize_history(all_chunks[:2])
111
- for response in chat_inf(message, history, seed, temp, tokens, top_p, rep_p):
112
- chat.append(*response)
113
-
114
- go = btn.click(on_chat, [inp, chat, seed, temp, tokens, top_p, rep_p], chat)
115
 
116
  stop_btn.click(None, None, None, cancels=[go])
117
  clear_btn.click(clear_fn, None, [inp, chat])
 
10
  # Embedded system prompt
11
  system_prompt_text = "You are a smart and helpful co-worker of Thailand based multi-national company PTT, and PTTEP. You help with any kind of request and provide a detailed answer to the question. But if you are asked about something unethical or dangerous, you must refuse and provide a safe and respectful way to handle that."
12
 
13
+ # Read the content of the info.md file
14
  with open("info.md", "r") as file:
15
  info_md_content = file.read()
16
 
17
+ # Chunk the info.md content into smaller sections
18
+ chunk_size = 2500 # Adjust this size as needed
 
 
 
19
  info_md_chunks = textwrap.wrap(info_md_content, chunk_size)
 
 
 
 
20
 
21
+ def get_all_chunks(chunks):
22
+ return "\n\n".join(chunks)
 
 
 
 
23
 
24
+ def format_prompt_mixtral(message, history, info_md_chunks):
 
 
 
25
  prompt = "<s>"
26
+ all_chunks = get_all_chunks(info_md_chunks)
27
+ prompt += f"{all_chunks}\n\n" # Add all chunks of info.md at the beginning
28
  prompt += f"{system_prompt_text}\n\n" # Add the system prompt
29
 
30
+ if history:
31
+ for user_prompt, bot_response in history:
32
+ prompt += f"[INST] {user_prompt} [/INST]"
33
+ prompt += f" {bot_response}</s> "
 
34
  prompt += f"[INST] {message} [/INST]"
35
  return prompt
36
 
37
+ def chat_inf(prompt, history, seed, temp, tokens, top_p, rep_p):
38
  generate_kwargs = dict(
39
  temperature=temp,
40
  max_new_tokens=tokens,
 
44
  seed=seed,
45
  )
46
 
47
+ formatted_prompt = format_prompt_mixtral(prompt, history, info_md_chunks)
48
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
49
  output = ""
50
  for response in stream:
51
  output += response.token.text
52
+ yield [(prompt, output)]
53
+ history.append((prompt, output))
54
  yield history
55
 
56
  def clear_fn():
 
 
57
  return None, None
58
 
59
  rand_val = random.randint(1, 1111111111111111)
60
 
61
+ def check_rand(inp, val):
62
+ if inp:
63
+ return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=random.randint(1, 1111111111111111))
64
  else:
65
+ return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=int(val))
66
 
67
+ with gr.Blocks() as app: # Add auth here
68
  gr.HTML("""<center><h1 style='font-size:xx-large;'>PTT Chatbot</h1><br><h3>running on Huggingface Inference </h3><br><h7>EXPERIMENTAL</center>""")
69
  with gr.Row():
70
  chat = gr.Chatbot(height=500)
 
90
 
91
  hid1 = gr.Number(value=1, visible=False)
92
 
93
+ go = btn.click(check_rand, [rand, seed], seed).then(chat_inf, [inp, chat, seed, temp, tokens, top_p, rep_p], chat)
 
 
 
 
 
 
94
 
95
  stop_btn.click(None, None, None, cancels=[go])
96
  clear_btn.click(clear_fn, None, [inp, chat])