yongyi169 commited on
Commit
94bc373
·
verified ·
1 Parent(s): 766aa86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -4
app.py CHANGED
@@ -1,7 +1,111 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import random
4
 
5
+ models = [
6
+ "yongyi169/yy-gemma-it-ar-20240327",
7
+ "google/gemma-7b",
8
+ "google/gemma-7b-it",
9
+ "google/gemma-2b",
10
+ "google/gemma-2b-it"
11
+ ]
12
 
13
+ clients = []
14
+ for model in models:
15
+ clients.append(InferenceClient(model))
16
+
17
+
18
+ def format_prompt(message, history):
19
+ prompt = ""
20
+ if history:
21
+ for user_prompt, bot_response in history:
22
+ prompt += f"<start_of_turn>user{user_prompt}<end_of_turn>"
23
+ prompt += f"<start_of_turn>model{bot_response}"
24
+ prompt += f"<start_of_turn>user{message}<end_of_turn><start_of_turn>model"
25
+ return prompt
26
+
27
+
28
+ def chat_inf(system_prompt, prompt, history, client_choice, seed, temp, tokens, top_p, rep_p):
29
+ client = clients[int(client_choice) - 1]
30
+ if not history:
31
+ history = []
32
+ hist_len = 0
33
+ if history:
34
+ hist_len = len(history)
35
+ print(hist_len)
36
+
37
+ generate_kwargs = dict(
38
+ temperature=temp,
39
+ max_new_tokens=tokens,
40
+ top_p=top_p,
41
+ repetition_penalty=rep_p,
42
+ do_sample=True,
43
+ seed=seed,
44
+ )
45
+ formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
46
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True,
47
+ return_full_text=False)
48
+ output = ""
49
+
50
+ for response in stream:
51
+ output += response.token.text
52
+ yield [(prompt, output)]
53
+ history.append((prompt, output))
54
+ yield history
55
+
56
+
57
+ def clear_fn():
58
+ return None
59
+
60
+
61
+ rand_val = random.randint(1, 1111111111111111)
62
+
63
+
64
+ def check_rand(inp, val):
65
+ if inp is True:
66
+ return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=random.randint(1, 1111111111111111))
67
+ else:
68
+ return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=int(val))
69
+
70
+
71
+ with gr.Blocks() as app:
72
+ gr.HTML(
73
+ """<center><h1 style='font-size:xx-large;'>Google Gemma Models</h1></center>""")
74
+ with gr.Group():
75
+ with gr.Row():
76
+ client_choice = gr.Dropdown(label="Models", type='index', choices=[c for c in models], value=models[0],
77
+ interactive=True)
78
+ chat_b = gr.Chatbot(height=500)
79
+ with gr.Group():
80
+ with gr.Row():
81
+ with gr.Column(scale=1):
82
+ with gr.Group():
83
+ rand = gr.Checkbox(label="Random Seed", value=True)
84
+ seed = gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, step=1, value=rand_val)
85
+ tokens = gr.Slider(label="Max new tokens", value=6400, minimum=0, maximum=8000, step=64,
86
+ interactive=True, visible=True, info="The maximum number of tokens")
87
+ with gr.Column(scale=1):
88
+ with gr.Group():
89
+ temp = gr.Slider(label="Temperature", step=0.01, minimum=0.01, maximum=1.0, value=0.9)
90
+ top_p = gr.Slider(label="Top-P", step=0.01, minimum=0.01, maximum=1.0, value=0.9)
91
+ rep_p = gr.Slider(label="Repetition Penalty", step=0.1, minimum=0.1, maximum=2.0, value=1.0)
92
+
93
+ with gr.Group():
94
+ with gr.Row():
95
+ with gr.Column(scale=3):
96
+ sys_inp = gr.Textbox(label="System Prompt (optional)")
97
+ inp = gr.Textbox(label="Prompt")
98
+ with gr.Row():
99
+ btn = gr.Button("Chat")
100
+ stop_btn = gr.Button("Stop")
101
+ clear_btn = gr.Button("Clear")
102
+
103
+ chat_sub = inp.submit(check_rand, [rand, seed], seed).then(chat_inf,
104
+ [sys_inp, inp, chat_b, client_choice, seed, temp, tokens,
105
+ top_p, rep_p], chat_b)
106
+ go = btn.click(check_rand, [rand, seed], seed).then(chat_inf,
107
+ [sys_inp, inp, chat_b, client_choice, seed, temp, tokens, top_p,
108
+ rep_p], chat_b)
109
+ stop_btn.click(None, None, None, cancels=[go, chat_sub])
110
+ clear_btn.click(clear_fn, None, [chat_b])
111
+ app.queue(default_concurrency_limit=10).launch()