basit123796 commited on
Commit
708432b
·
verified ·
1 Parent(s): 4e478e2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio_client import Client
3
+ from huggingface_hub import InferenceClient
4
+
5
+ ss_client = Client("https://omnibus-html-image-current-tab.hf.space/")
6
+
7
+ models=[
8
+ "google/gemma-7b",
9
+ "google/gemma-7b-it",
10
+ "google/gemma-2b",
11
+ "google/gemma-2b-it"
12
+ ]
13
+ clients=[
14
+ InferenceClient(models[0]),
15
+ InferenceClient(models[1]),
16
+ InferenceClient(models[2]),
17
+ InferenceClient(models[3]),
18
+ ]
19
+
20
+ VERBOSE=False
21
+
22
+ def load_models(inp):
23
+ if VERBOSE==True:
24
+ print(type(inp))
25
+ print(inp)
26
+ print(models[inp])
27
+ return gr.update(label=models[inp])
28
+
29
+ def format_prompt(message, history, cust_p):
30
+ prompt = ""
31
+ if history:
32
+ for user_prompt, bot_response in history:
33
+ prompt += f"<start_of_turn>user{user_prompt}<end_of_turn>"
34
+ prompt += f"<start_of_turn>model{bot_response}<end_of_turn>"
35
+ if VERBOSE==True:
36
+ print(prompt)
37
+ prompt+=cust_p.replace("USER_INPUT",message)
38
+ return prompt
39
+
40
+ def chat_inf(system_prompt,prompt,history,memory,client_choice,seed,temp,tokens,top_p,rep_p,chat_mem,cust_p):
41
+ hist_len=0
42
+ client=clients[int(client_choice)-1]
43
+ if not history:
44
+ history = []
45
+ hist_len=0
46
+ if not memory:
47
+ memory = []
48
+ mem_len=0
49
+ if memory:
50
+ for ea in memory[0-chat_mem:]:
51
+ hist_len+=len(str(ea))
52
+ in_len=len(system_prompt+prompt)+hist_len
53
+
54
+ if (in_len+tokens) > 8000:
55
+ history.append((prompt,"Wait, that's too many tokens, please reduce the 'Chat Memory' value, or reduce the 'Max new tokens' value"))
56
+ yield history,memory
57
+ else:
58
+ generate_kwargs = dict(
59
+ temperature=temp,
60
+ max_new_tokens=tokens,
61
+ top_p=top_p,
62
+ repetition_penalty=rep_p,
63
+ do_sample=True,
64
+ seed=seed,
65
+ )
66
+ if system_prompt:
67
+ formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", memory[0-chat_mem:],cust_p)
68
+ else:
69
+ formatted_prompt = format_prompt(prompt, memory[0-chat_mem:],cust_p)
70
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
71
+ output = ""
72
+ for response in stream:
73
+ output += response.token.text
74
+ yield [(prompt,output)],memory
75
+ history.append((prompt,output))
76
+ memory.append((prompt,output))
77
+ yield history,memory
78
+
79
+ if VERBOSE==True:
80
+ print("\n######### HIST "+str(in_len))
81
+ print("\n######### TOKENS "+str(tokens))
82
+
83
+ def get_screenshot(chat: list,height=5000,width=600,chatblock=[],theme="light",wait=3000,header=True):
84
+ tog = 0
85
+ if chatblock:
86
+ tog = 3
87
+ result = ss_client.predict(str(chat),height,width,chatblock,header,theme,wait,api_name="/run_script")
88
+ out = f'https://omnibus-html-image-current-tab.hf.space/file={result[tog]}'
89
+ return out
90
+
91
+ def clear_fn():
92
+ return None,None,None,None
93
+ rand_val=random.randint(1,1111111111111111)
94
+
95
+ def check_rand(inp,val):
96
+ if inp==True:
97
+ return random.randint(1,1111111111111111)
98
+ else:
99
+ return int(val)
100
+
101
+ with gr.Blocks() as app:
102
+ memory=gr.State()
103
+ chat_b = gr.Chatbot(height=500)
104
+ with gr.Group():
105
+ with gr.Row():
106
+ with gr.Column(scale=3):
107
+ inp = gr.Textbox(label="Prompt")
108
+ sys_inp = gr.Textbox(label="System Prompt (optional)")
109
+ custom_prompt=gr.Textbox(label="Modify Prompt Format", info="For testing purposes. 'USER_INPUT' is where 'SYSTEM_PROMPT, PROMPT' will be placed", lines=3,value="<start_of_turn>userUSER_INPUT<end_of_turn><start_of_turn>model")
110
+ btn = gr.Button("Chat")
111
+ with gr.Column(scale=1):
112
+ with gr.Group():
113
+ rand = gr.Checkbox(label="Random Seed", value=True)
114
+ seed=gr.Slider(label="Seed", minimum=1, maximum=1111111111111111,step=1, value=rand_val)
115
+ tokens = gr.Slider(label="Max new tokens",value=1600,minimum=0,maximum=8000,step=64,interactive=True, visible=True,info="The maximum number of tokens")
116
+ temp=gr.Slider(label="Temperature",step=0.01, minimum=0.01, maximum=1.0, value=0.49)
117
+ top_p=gr.Slider(label="Top-P",step=0.01, minimum=0.01, maximum=1.0, value=0.49)
118
+ rep_p=gr.Slider(label="Repetition Penalty",step=0.01, minimum=0.1, maximum=2.0, value=0.99)
119
+ chat_mem=gr.Number(label="Chat Memory", info="Number of previous chats to retain",value=4)
120
+
121
+ client_choice=gr.Dropdown(label="Models",type='index',choices=[c for c in models],value=models[0],interactive=True)
122
+ client_choice.change(load_models,client_choice,[chat_b])
123
+ app.load(load_models,client_choice,[chat_b])
124
+
125
+ chat_sub=inp.submit(check_rand,[rand,seed],seed).then(chat_inf,[sys_inp,inp,chat_b,memory,client_choice,seed,temp,tokens,top_p,rep_p,chat_mem,custom_prompt],[chat_b,memory])
126
+ go=btn.click(check_rand,[rand,seed],seed).then(chat_inf,[sys_inp,inp,chat_b,memory,client_choice,seed,temp,tokens,top_p,rep_p,chat_mem,custom_prompt],[chat_b,memory])
127
+
128
+ app.queue(default_concurrency_limit=10).launch()