rishiraj commited on
Commit
cd7c7be
1 Parent(s): df3bc75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -122
app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  import os
4
  import re
5
  import requests
 
6
  import http.client
7
  import typing
8
  import urllib.request
@@ -40,114 +41,6 @@ def search(url):
40
  response = model.generate_content([image,"Describe what is shown in this image."])
41
  return response.text
42
 
43
- # def format_prompt(message, history, cust_p):
44
- # prompt = ""
45
- # for user_prompt, bot_response in history:
46
- # prompt += f"<start_of_turn>user{user_prompt}<end_of_turn>"
47
- # prompt += f"<start_of_turn>model{bot_response}<end_of_turn>"
48
- # prompt += cust_p.replace("USER_INPUT",message)
49
- # return prompt
50
-
51
- # def generate(
52
- # prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
53
- # ):
54
- # custom_prompt="<start_of_turn>userUSER_INPUT<end_of_turn><start_of_turn>model"
55
- # temperature = float(temperature)
56
- # if temperature < 1e-2:
57
- # temperature = 1e-2
58
- # top_p = float(top_p)
59
-
60
- # generate_kwargs = dict(
61
- # temperature=temperature,
62
- # max_new_tokens=max_new_tokens,
63
- # top_p=top_p,
64
- # repetition_penalty=repetition_penalty,
65
- # do_sample=True,
66
- # seed=42,
67
- # )
68
-
69
- # image = extract_image_urls(prompt)
70
- # if image:
71
- # image_description = "Image Description: " + search(image)
72
- # prompt = prompt.replace(image, image_description)
73
- # print(prompt)
74
- # formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history, custom_prompt)
75
- # stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
76
- # output = ""
77
-
78
- # for response in stream:
79
- # output += response.token.text
80
- # yield output
81
- # return output
82
-
83
-
84
- # additional_inputs=[
85
- # gr.Textbox(
86
- # label="System Prompt",
87
- # max_lines=1,
88
- # interactive=True,
89
- # ),
90
- # gr.Slider(
91
- # label="Temperature",
92
- # value=0.9,
93
- # minimum=0.0,
94
- # maximum=1.0,
95
- # step=0.05,
96
- # interactive=True,
97
- # info="Higher values produce more diverse outputs",
98
- # ),
99
- # gr.Slider(
100
- # label="Max new tokens",
101
- # value=256,
102
- # minimum=0,
103
- # maximum=1048,
104
- # step=64,
105
- # interactive=True,
106
- # info="The maximum numbers of new tokens",
107
- # ),
108
- # gr.Slider(
109
- # label="Top-p (nucleus sampling)",
110
- # value=0.90,
111
- # minimum=0.0,
112
- # maximum=1,
113
- # step=0.05,
114
- # interactive=True,
115
- # info="Higher values sample more low-probability tokens",
116
- # ),
117
- # gr.Slider(
118
- # label="Repetition penalty",
119
- # value=1.2,
120
- # minimum=1.0,
121
- # maximum=2.0,
122
- # step=0.05,
123
- # interactive=True,
124
- # info="Penalize repeated tokens",
125
- # )
126
- # ]
127
-
128
- # examples=[["What are they doing here https://upload.wikimedia.org/wikipedia/commons/3/38/Two_dancers.jpg ?", None, None, None, None, None]]
129
-
130
- # gr.ChatInterface(
131
- # fn=generate,
132
- # chatbot=gr.Chatbot(show_label=True, show_share_button=True, show_copy_button=True, likeable=True, layout="bubble", bubble_full_width=False),
133
- # additional_inputs=additional_inputs,
134
- # title="Gemma Gemini Multimodal Chatbot",
135
- # description="Gemini Sprint submission by Rishiraj Acharya. Uses Google's Gemini 1.0 Pro Vision multimodal model from Vertex AI with Google's Gemma 7B Instruct model from Hugging Face. Google Cloud credits are provided for this project.",
136
- # theme="Soft",
137
- # examples=examples,
138
- # concurrency_limit=20,
139
- # ).launch(show_api=False)
140
-
141
-
142
-
143
-
144
- import random
145
-
146
-
147
-
148
- # def load_models(inp):
149
- # return gr.update(label=models[inp])
150
-
151
  def format_prompt(message, history, cust_p):
152
  prompt = ""
153
  if history:
@@ -212,7 +105,7 @@ def check_rand(inp,val):
212
  else:
213
  return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=int(val))
214
 
215
- with gr.Blocks() as app:
216
  memory=gr.State()
217
  gr.HTML("""<center><h1 style='font-size:xx-large;'>Gemma Gemini Multimodal Chatbot</h1><br><h3>Gemini Sprint submission by Rishiraj Acharya. Uses Google's Gemini 1.0 Pro Vision multimodal model from Vertex AI with Google's Gemma 7B Instruct model from Hugging Face. Google Cloud credits are provided for this project.</h3>""")
218
  chat_b = gr.Chatbot(show_label=True, show_share_button=True, show_copy_button=True, likeable=True, layout="bubble", bubble_full_width=False)
@@ -221,16 +114,11 @@ with gr.Blocks() as app:
221
  with gr.Column(scale=3):
222
  inp = gr.Textbox(label="Prompt")
223
  sys_inp = gr.Textbox(label="System Prompt (optional)")
224
- with gr.Accordion("Prompt Format",open=False):
225
- 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")
226
  with gr.Row():
227
- with gr.Column(scale=2):
228
- btn = gr.Button("Chat")
229
- with gr.Column(scale=1):
230
- with gr.Group():
231
- stop_btn=gr.Button("Stop")
232
- clear_btn=gr.Button("Clear")
233
- # client_choice=gr.Dropdown(label="Models",type='index',choices=[c for c in models],value=models[0],interactive=True)
234
  with gr.Column(scale=1):
235
  with gr.Group():
236
  rand = gr.Checkbox(label="Random Seed", value=True)
@@ -240,10 +128,6 @@ with gr.Blocks() as app:
240
  top_p=gr.Slider(label="Top-P",step=0.01, minimum=0.01, maximum=1.0, value=0.49)
241
  rep_p=gr.Slider(label="Repetition Penalty",step=0.01, minimum=0.1, maximum=2.0, value=0.99)
242
  chat_mem=gr.Number(label="Chat Memory", info="Number of previous chats to retain",value=4)
243
-
244
-
245
- # client_choice.change(load_models,client_choice,[chat_b])
246
- # app.load(load_models,client_choice,[chat_b])
247
 
248
  chat_sub=inp.submit(check_rand,[rand,seed],seed).then(chat_inf,[sys_inp,inp,chat_b,memory,seed,temp,tokens,top_p,rep_p,chat_mem,custom_prompt],[chat_b,memory])
249
  go=btn.click(check_rand,[rand,seed],seed).then(chat_inf,[sys_inp,inp,chat_b,memory,seed,temp,tokens,top_p,rep_p,chat_mem,custom_prompt],[chat_b,memory])
 
3
  import os
4
  import re
5
  import requests
6
+ import random
7
  import http.client
8
  import typing
9
  import urllib.request
 
41
  response = model.generate_content([image,"Describe what is shown in this image."])
42
  return response.text
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def format_prompt(message, history, cust_p):
45
  prompt = ""
46
  if history:
 
105
  else:
106
  return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=int(val))
107
 
108
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
109
  memory=gr.State()
110
  gr.HTML("""<center><h1 style='font-size:xx-large;'>Gemma Gemini Multimodal Chatbot</h1><br><h3>Gemini Sprint submission by Rishiraj Acharya. Uses Google's Gemini 1.0 Pro Vision multimodal model from Vertex AI with Google's Gemma 7B Instruct model from Hugging Face. Google Cloud credits are provided for this project.</h3>""")
111
  chat_b = gr.Chatbot(show_label=True, show_share_button=True, show_copy_button=True, likeable=True, layout="bubble", bubble_full_width=False)
 
114
  with gr.Column(scale=3):
115
  inp = gr.Textbox(label="Prompt")
116
  sys_inp = gr.Textbox(label="System Prompt (optional)")
117
+ custom_prompt="<start_of_turn>userUSER_INPUT<end_of_turn><start_of_turn>model"
 
118
  with gr.Row():
119
+ btn = gr.Button("Chat")
120
+ stop_btn = gr.Button("Stop")
121
+ clear_btn = gr.Button("Clear")
 
 
 
 
122
  with gr.Column(scale=1):
123
  with gr.Group():
124
  rand = gr.Checkbox(label="Random Seed", value=True)
 
128
  top_p=gr.Slider(label="Top-P",step=0.01, minimum=0.01, maximum=1.0, value=0.49)
129
  rep_p=gr.Slider(label="Repetition Penalty",step=0.01, minimum=0.1, maximum=2.0, value=0.99)
130
  chat_mem=gr.Number(label="Chat Memory", info="Number of previous chats to retain",value=4)
 
 
 
 
131
 
132
  chat_sub=inp.submit(check_rand,[rand,seed],seed).then(chat_inf,[sys_inp,inp,chat_b,memory,seed,temp,tokens,top_p,rep_p,chat_mem,custom_prompt],[chat_b,memory])
133
  go=btn.click(check_rand,[rand,seed],seed).then(chat_inf,[sys_inp,inp,chat_b,memory,seed,temp,tokens,top_p,rep_p,chat_mem,custom_prompt],[chat_b,memory])