negismohit123 commited on
Commit
85225a7
·
verified ·
1 Parent(s): f27635d

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +109 -145
main.py CHANGED
@@ -1,146 +1,110 @@
1
- # import gradio as gr
2
- # from huggingface_hub import InferenceClient
3
- # import random
4
-
5
- # models = [
6
- # "google/gemma-7b",
7
- # "google/gemma-7b-it",
8
- # "google/gemma-2b",
9
- # "google/gemma-2b-it"
10
- # ]
11
-
12
- # clients = []
13
- # for model in models:
14
- # clients.append(InferenceClient(model))
15
-
16
-
17
- # def format_prompt(message, history):
18
- # prompt = ""
19
- # if history:
20
- # for user_prompt, bot_response in history:
21
- # prompt += f"<start_of_turn>user{user_prompt}<end_of_turn>"
22
- # prompt += f"<start_of_turn>model{bot_response}"
23
- # prompt += f"<start_of_turn>user{message}<end_of_turn><start_of_turn>model"
24
- # return prompt
25
-
26
-
27
- # def chat_inf(system_prompt, prompt, history, client_choice, seed, temp, tokens, top_p, rep_p):
28
- # client = clients[int(client_choice) - 1]
29
- # if not history:
30
- # history = []
31
- # hist_len = 0
32
- # if history:
33
- # hist_len = len(history)
34
- # print(hist_len)
35
-
36
- # generate_kwargs = dict(
37
- # temperature=temp,
38
- # max_new_tokens=tokens,
39
- # top_p=top_p,
40
- # repetition_penalty=rep_p,
41
- # do_sample=True,
42
- # seed=seed,
43
- # )
44
- # formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
45
- # stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True,
46
- # return_full_text=False)
47
- # output = ""
48
-
49
- # for response in stream:
50
- # output += response.token.text
51
- # yield [(prompt, output)]
52
- # history.append((prompt, output))
53
- # yield history
54
-
55
-
56
- # def clear_fn():
57
- # return None
58
-
59
-
60
- # rand_val = random.randint(1, 1111111111111111)
61
-
62
-
63
- # def check_rand(inp, val):
64
- # if inp is True:
65
- # return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=random.randint(1, 1111111111111111))
66
- # else:
67
- # return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=int(val))
68
-
69
-
70
- # with gr.Blocks() as app:
71
- # gr.HTML(
72
- # """<center><h1 style='font-size:xx-large;'>Google Gemma Models</h1></center>""")
73
- # with gr.Group():
74
- # with gr.Row():
75
- # client_choice = gr.Dropdown(label="Models", type='index', choices=[c for c in models], value=models[0],
76
- # interactive=True)
77
- # chat_b = gr.Chatbot(height=500)
78
- # with gr.Group():
79
- # with gr.Row():
80
- # with gr.Column(scale=1):
81
- # with gr.Group():
82
- # rand = gr.Checkbox(label="Random Seed", value=True)
83
- # seed = gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, step=1, value=rand_val)
84
- # tokens = gr.Slider(label="Max new tokens", value=6400, minimum=0, maximum=8000, step=64,
85
- # interactive=True, visible=True, info="The maximum number of tokens")
86
- # with gr.Column(scale=1):
87
- # with gr.Group():
88
- # temp = gr.Slider(label="Temperature", step=0.01, minimum=0.01, maximum=1.0, value=0.9)
89
- # top_p = gr.Slider(label="Top-P", step=0.01, minimum=0.01, maximum=1.0, value=0.9)
90
- # rep_p = gr.Slider(label="Repetition Penalty", step=0.1, minimum=0.1, maximum=2.0, value=1.0)
91
-
92
- # with gr.Group():
93
- # with gr.Row():
94
- # with gr.Column(scale=3):
95
- # sys_inp = gr.Textbox(label="System Prompt (optional)")
96
- # inp = gr.Textbox(label="Prompt")
97
- # with gr.Row():
98
- # btn = gr.Button("Chat")
99
- # stop_btn = gr.Button("Stop")
100
- # clear_btn = gr.Button("Clear")
101
-
102
- # chat_sub = inp.submit(check_rand, [rand, seed], seed).then(chat_inf,
103
- # [sys_inp, inp, chat_b, client_choice, seed, temp, tokens,
104
- # top_p, rep_p], chat_b)
105
- # go = btn.click(check_rand, [rand, seed], seed).then(chat_inf,
106
- # [sys_inp, inp, chat_b, client_choice, seed, temp, tokens, top_p,
107
- # rep_p], chat_b)
108
- # stop_btn.click(None, None, None, cancels=[go, chat_sub])
109
- # clear_btn.click(clear_fn, None, [chat_b])
110
- # app.queue(default_concurrency_limit=10).launch()
111
  import gradio as gr
112
- import pandas as pd
113
- import re
114
- import time
115
- import plotly.express as px
116
-
117
- class CSVFileUploader:
118
- def __init__(self):
119
- self.file = None
120
- self.selected_column = None
121
- self.selected_row = None
122
-
123
- def upload_file(self, file):
124
- self.file = pd.read_csv(file.name)
125
- df = self.file
126
-
127
- data_preview = df.head()
128
- data_info = df.info()
129
- descriptive_statistics = df.describe()
130
-
131
- return data_preview, data_info, descriptive_statistics
132
-
133
- def main(file):
134
- uploader = CSVFileUploader()
135
- data_preview, data_info, descriptive_statistics = uploader.upload_file(file)
136
- return data_preview, data_info, descriptive_statistics
137
-
138
- iface = gr.Interface(fn=main,
139
- inputs="file",
140
- outputs=["text", "text", "text"],
141
- title="Gemma CSV Infer",
142
- description="Upload a CSV file to preview its data, view information, and descriptive statistics.")
143
- iface.launch(share=True)
144
-
145
- # -----------------------------------------------------------------------
146
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import random
4
+
5
+ models = [
6
+ "google/gemma-7b",
7
+ "google/gemma-7b-it",
8
+ "google/gemma-2b",
9
+ "google/gemma-2b-it"
10
+ ]
11
+
12
+ clients = []
13
+ for model in models:
14
+ clients.append(InferenceClient(model))
15
+
16
+
17
+ def format_prompt(message, history):
18
+ prompt = ""
19
+ if history:
20
+ for user_prompt, bot_response in history:
21
+ prompt += f"<start_of_turn>user{user_prompt}<end_of_turn>"
22
+ prompt += f"<start_of_turn>model{bot_response}"
23
+ prompt += f"<start_of_turn>user{message}<end_of_turn><start_of_turn>model"
24
+ return prompt
25
+
26
+
27
+ def chat_inf(system_prompt, prompt, history, client_choice, seed, temp, tokens, top_p, rep_p):
28
+ client = clients[int(client_choice) - 1]
29
+ if not history:
30
+ history = []
31
+ hist_len = 0
32
+ if history:
33
+ hist_len = len(history)
34
+ print(hist_len)
35
+
36
+ generate_kwargs = dict(
37
+ temperature=temp,
38
+ max_new_tokens=tokens,
39
+ top_p=top_p,
40
+ repetition_penalty=rep_p,
41
+ do_sample=True,
42
+ seed=seed,
43
+ )
44
+ formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
45
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True,
46
+ return_full_text=False)
47
+ output = ""
48
+
49
+ for response in stream:
50
+ output += response.token.text
51
+ yield [(prompt, output)]
52
+ history.append((prompt, output))
53
+ yield history
54
+
55
+
56
+ def clear_fn():
57
+ return None
58
+
59
+
60
+ rand_val = random.randint(1, 1111111111111111)
61
+
62
+
63
+ def check_rand(inp, val):
64
+ if inp is True:
65
+ return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=random.randint(1, 1111111111111111))
66
+ else:
67
+ return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=int(val))
68
+
69
+
70
+ with gr.Blocks() as app:
71
+ gr.HTML(
72
+ """<center><h1 style='font-size:xx-large;'>Google Gemma Models</h1></center>""")
73
+ with gr.Group():
74
+ with gr.Row():
75
+ client_choice = gr.Dropdown(label="Models", type='index', choices=[c for c in models], value=models[0],
76
+ interactive=True)
77
+ chat_b = gr.Chatbot(height=500)
78
+ with gr.Group():
79
+ with gr.Row():
80
+ with gr.Column(scale=1):
81
+ with gr.Group():
82
+ rand = gr.Checkbox(label="Random Seed", value=True)
83
+ seed = gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, step=1, value=rand_val)
84
+ tokens = gr.Slider(label="Max new tokens", value=6400, minimum=0, maximum=8000, step=64,
85
+ interactive=True, visible=True, info="The maximum number of tokens")
86
+ with gr.Column(scale=1):
87
+ with gr.Group():
88
+ temp = gr.Slider(label="Temperature", step=0.01, minimum=0.01, maximum=1.0, value=0.9)
89
+ top_p = gr.Slider(label="Top-P", step=0.01, minimum=0.01, maximum=1.0, value=0.9)
90
+ rep_p = gr.Slider(label="Repetition Penalty", step=0.1, minimum=0.1, maximum=2.0, value=1.0)
91
+
92
+ with gr.Group():
93
+ with gr.Row():
94
+ with gr.Column(scale=3):
95
+ sys_inp = gr.Textbox(label="System Prompt (optional)")
96
+ inp = gr.Textbox(label="Prompt")
97
+ with gr.Row():
98
+ btn = gr.Button("Chat")
99
+ stop_btn = gr.Button("Stop")
100
+ clear_btn = gr.Button("Clear")
101
+
102
+ chat_sub = inp.submit(check_rand, [rand, seed], seed).then(chat_inf,
103
+ [sys_inp, inp, chat_b, client_choice, seed, temp, tokens,
104
+ top_p, rep_p], chat_b)
105
+ go = btn.click(check_rand, [rand, seed], seed).then(chat_inf,
106
+ [sys_inp, inp, chat_b, client_choice, seed, temp, tokens, top_p,
107
+ rep_p], chat_b)
108
+ stop_btn.click(None, None, None, cancels=[go, chat_sub])
109
+ clear_btn.click(clear_fn, None, [chat_b])
110
+ app.queue(default_concurrency_limit=10).launch()