broadfield commited on
Commit
271f307
·
verified ·
1 Parent(s): 1a5e11b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
2
+ import gradio as gr
3
+ import random
4
+ import prompts
5
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
6
+
7
+ def format_prompt(message, history):
8
+
9
+ prompt = "<s>"
10
+ for user_prompt, bot_response in history:
11
+ print (bot_response)
12
+ prompt += f"[INST] {user_prompt} [/INST]"
13
+ prompt += f" {bot_response}</s> "
14
+ prompt += f"[INST] {message} [/INST]"
15
+ return prompt
16
+
17
+ def generate(prompt,history,max_new_tokens,seed):
18
+ #seed = random.randint(1,9999999999999)
19
+ print(seed)
20
+ system_prompt = prompts.SONG_WRITER.format(history=history)
21
+ generate_kwargs = dict(
22
+ temperature=0.9,
23
+ max_new_tokens=max_new_tokens,
24
+ top_p=0.95,
25
+ repetition_penalty=1.0,
26
+ do_sample=True,
27
+ seed=seed,
28
+ )
29
+
30
+ formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
31
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
32
+ output = ""
33
+
34
+ for response in stream:
35
+ output += response.token.text
36
+ yield output
37
+
38
+ additional_inputs = [
39
+ gr.Slider(
40
+ label="Max new tokens",
41
+ value=4096,
42
+ minimum=10,
43
+ maximum=1048*10,
44
+ step=64,
45
+ interactive=True,
46
+ info="The maximum numbers of words the chatbot will return",
47
+ ),
48
+
49
+ gr.Slider(
50
+ label="Seed",
51
+ value=random.randint(1,9999999999999999),
52
+ minimum=0,
53
+ maximum=9999999999999999,
54
+ step=64,
55
+ interactive=True,
56
+ info="Each seed produces a different output to a single prompt",
57
+ ),
58
+ ]
59
+ examples=[["Write a heavy metal rock song about horses (1000 words)"],
60
+ ["Write a really long song about an elephant love story. The elephants have been together for a long time. (3000 words)"],
61
+ ["Write a slammin rap song about about a gangster fising trip."],
62
+ ["Use the same lyrics as above, but add more words to each verse"],
63
+ ]
64
+ gr.ChatInterface(
65
+ fn=generate,
66
+ chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, layout="panel"),
67
+ title="Song Writer",
68
+ additional_inputs=additional_inputs,
69
+ examples=examples,
70
+ concurrency_limit=20,
71
+ ).launch(show_api=False)