taras5500 commited on
Commit
b759b69
1 Parent(s): edf6935

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
2
+ import gradio as gr
3
+
4
+ client = InferenceClient(
5
+ "mistralai/Mixtral-8x7B-Instruct-v0.1"
6
+ )
7
+
8
+ def character_prompt(dict, max_new_tokens):
9
+ system_prompt = f"ensure responses are no longer than {max_new_tokens} tokens."
10
+ for key, value in dict.items():
11
+ system_prompt += f"{key}: {value}."
12
+ return f"answer me like the main character of a book with this description {system_prompt}"
13
+
14
+ def generate(prompt, history, name, description, user_name, max_new_tokens):
15
+ generate_kwargs = dict(
16
+ temperature=0.9,
17
+ max_new_tokens=max_new_tokens,
18
+ top_p=0.95,
19
+ repetition_penalty=1.0,
20
+ do_sample=True,
21
+ )
22
+ system_setting = character_prompt({"Your name": name, "Your description": description, "how to call me": user_name}, max_new_tokens)
23
+ formatted_prompt = format_prompt(prompt, history, system_setting)
24
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
25
+ output = ""
26
+
27
+ for response in stream:
28
+ output += response.token.text
29
+ yield output
30
+ return output
31
+
32
+ def format_prompt(prompt, history, system_setting):
33
+ formatted_prompt = "<s>"
34
+ for user_prompt, bot_response in history:
35
+ formatted_prompt += f"[INST] {user_prompt} [/INST] {bot_response} </s> "
36
+ formatted_prompt += f"[INST] {system_setting}, {prompt} [/INST]"
37
+ return formatted_prompt
38
+
39
+ additional_inputs = [
40
+ gr.Textbox(
41
+ label="Name",
42
+ max_lines=1,
43
+ interactive=True,
44
+ ),
45
+ gr.Textbox(
46
+ label="Description",
47
+ max_lines=1,
48
+ interactive=True,
49
+ ),
50
+ gr.Textbox(
51
+ label="How to call you",
52
+ max_lines=1,
53
+ interactive=True,
54
+ ),
55
+ gr.Slider(
56
+ label="Max new tokens",
57
+ value=256,
58
+ minimum=0,
59
+ maximum=1048,
60
+ step=64,
61
+ interactive=True,
62
+ info="The maximum numbers of new tokens",
63
+ )
64
+ ]
65
+
66
+ examples = [
67
+ ["tell me about your day", "Messmer, the Impaler", "Messmer, son of Queen Marika the Eternal, was born with a dark serpent inside him, called the Abyssal Serpent, contained by Marika through a seal in place of one of his eyes. Constantly accompanied by winged snakes, Messmer and Commander Gaius acted as 'older brothers' to General Radahn. Before the Shattering, Marika tasked Messmer with purging the tower-dwelling people of the Land of Shadow. Even after being abandoned by Marika, he continued this purge with zeal. Among his army, Black Knight Commander Andreas and Black Knight Captain Huw, initially considered brothers-in-arms, eventually rebelled upon discovering his serpentine nature.","Marika",256 ],
68
+ ["tell me about your day","Ada Wong","Ada Wong's early life is shrouded in mystery, with unconfirmed details about her ethnicity, nationality, and birth. She has mentioned a possibly fabricated story of being born around 1974 in Saigon, Vietnam, to a wealthy family that fled to the United States post-Vietnam War. As a young adult, she engaged in criminal activities and was eventually recruited by Albert Wesker for a bioweapons corporation known as 'the Organization.' Around 1997, she infiltrated the Umbrella Corporation's Arklay Laboratory, forming a relationship with Dr. John Clemens to gather information. During the 1998 t-Virus outbreak, Wong managed to escape undetected despite being present at the facility.","Leon",64]
69
+ ]
70
+
71
+ custom_css = """
72
+ /* Dark mode styles */
73
+ body, .panel, .panel input, .panel select, .panel textarea, .panel button {
74
+ background-color: #2e2e2e;
75
+ color: #e0e0e0;
76
+ }
77
+ .panel {
78
+ border: 1px solid #555;
79
+ }
80
+ .panel input, .panel select, .panel textarea, .panel button {
81
+ border: 1px solid #555;
82
+ background-color: #3e3e3e;
83
+ color: #e0e0e0;
84
+ }
85
+ .panel input:focus, .panel select:focus, .panel textarea:focus, .panel button:focus {
86
+ border-color: #777;
87
+ }
88
+ .chat-message {
89
+ background-color: #444;
90
+ color: #e0e0e0;
91
+ }
92
+ .chat-message.user {
93
+ background-color: #333;
94
+ }
95
+ .chat-message.bot {
96
+ background-color: #444;
97
+ }
98
+ .chatbot .chat-message {
99
+ border: 1px solid #555;
100
+ }
101
+ .chatbot .chat-message p {
102
+ color: #e0e0e0;
103
+ }
104
+ """
105
+
106
+ iface = gr.ChatInterface(
107
+ fn=generate,
108
+ chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
109
+ additional_inputs=additional_inputs,
110
+ title="Mixtral 46.7B",
111
+ examples=examples,
112
+ concurrency_limit=20,
113
+ description="all fields are required",
114
+ css=custom_css # Add custom CSS here
115
+ )
116
+
117
+ iface.queue(api_open=True)
118
+ iface.launch(share=True, debug=True)