Mattral commited on
Commit
e19ab69
·
verified ·
1 Parent(s): 732e00c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -52
app.py CHANGED
@@ -1,64 +1,98 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import random
 
4
 
5
- # Initialize the model
6
  model = "mistralai/Mixtral-8x7B-Instruct-v0.1"
7
  client = InferenceClient(model)
8
 
9
- def chat_response(prompt, history, seed, temp, tokens, top_p, rep_p):
10
- generate_kwargs = {
11
- "temperature": temp,
12
- "max_new_tokens": tokens,
13
- "top_p": top_p,
14
- "repetition_penalty": rep_p,
15
- "do_sample": True,
16
- "seed": seed,
17
- }
18
-
19
- # Include the chat history in the prompt
20
- formatted_prompt = "\n".join([f"Q: {user_prompt}\nA: {bot_response}" for user_prompt, bot_response in history]) + f"\nQ: {prompt}\nA:"
21
-
22
- output = ""
23
-
24
- # Generating text in streaming mode
25
- for response in client.text_generation(formatted_prompt, **generate_kwargs, stream=True):
26
- # Assuming response is directly a string or contains a message
27
- output += response # Using response directly since it's a string
28
 
29
- # Yield the updated output for real-time display
30
- yield [(prompt, output)]
 
31
 
32
- # Append the full response to history after completion
33
- history.append((prompt, output))
34
- yield history # Yielding the updated history
35
-
36
- def clear_chat():
37
- return [], [] # Returning an empty history
38
-
39
- # Gradio interface
40
- with gr.Blocks() as app:
41
- gr.HTML("<center><h1>Chatbot</h1><h3>Ask your questions!</h3></center>")
42
-
43
- chat_box = gr.Chatbot(height=500)
44
- inp = gr.Textbox(label="Your Question", lines=5)
45
- btn = gr.Button("Ask")
46
- clear_btn = gr.Button("Clear")
47
-
48
- rand_seed = gr.Checkbox(label="Random Seed", value=True)
49
- seed_slider = gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=random.randint(1, 1111111111111111))
50
- tokens_slider = gr.Slider(label="Max new tokens", value=3840, minimum=0, maximum=8000)
51
- temp_slider = gr.Slider(label="Temperature", value=0.9, minimum=0.01, maximum=1.0)
52
- top_p_slider = gr.Slider(label="Top-P", value=0.9, minimum=0.01, maximum=1.0)
53
- rep_p_slider = gr.Slider(label="Repetition Penalty", value=1.0, minimum=0.1, maximum=2.0)
54
-
55
- # Handle button click to get chat response
56
- btn.click(
57
- lambda prompt: chat_response(prompt, [], seed_slider.value, temp_slider.value, tokens_slider.value, top_p_slider.value, rep_p_slider.value),
58
- inp,
59
- chat_box,
60
  )
61
 
62
- clear_btn.click(clear_chat, None, [inp, chat_box])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
- app.launch(share=True, auth=("admin", "0112358"))
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import random
4
+ import textwrap
5
 
6
+ # Define the model to be used
7
  model = "mistralai/Mixtral-8x7B-Instruct-v0.1"
8
  client = InferenceClient(model)
9
 
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,
41
+ top_p=top_p,
42
+ repetition_penalty=rep_p,
43
+ do_sample=True,
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)
71
+ with gr.Group():
72
+ with gr.Row():
73
+ with gr.Column(scale=3):
74
+ inp = gr.Textbox(label="Prompt", lines=5, interactive=True) # Increased lines and interactive
75
+ with gr.Row():
76
+ with gr.Column(scale=2):
77
+ btn = gr.Button("Chat")
78
+ with gr.Column(scale=1):
79
+ with gr.Group():
80
+ stop_btn = gr.Button("Stop")
81
+ clear_btn = gr.Button("Clear")
82
+ with gr.Column(scale=1):
83
+ with gr.Group():
84
+ rand = gr.Checkbox(label="Random Seed", value=True)
85
+ seed = gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, step=1, value=rand_val)
86
+ tokens = gr.Slider(label="Max new tokens", value=3840, minimum=0, maximum=8000, step=64, interactive=True, visible=True, info="The maximum number of tokens")
87
+ temp = gr.Slider(label="Temperature", step=0.01, minimum=0.01, maximum=1.0, value=0.9)
88
+ top_p = gr.Slider(label="Top-P", step=0.01, minimum=0.01, maximum=1.0, value=0.9)
89
+ rep_p = gr.Slider(label="Repetition Penalty", step=0.1, minimum=0.1, maximum=2.0, value=1.0)
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])
97
 
98
+ app.queue(default_concurrency_limit=10).launch(share=True, auth=("admin", "0112358"))