Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,6 +24,7 @@ from google.oauth2 import service_account
|
|
| 24 |
from googleapiclient.discovery import build
|
| 25 |
import wget
|
| 26 |
import urllib.request
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
def save_uploadedfile(uploadedfile):
|
|
@@ -38,6 +39,16 @@ stability_api = client.StabilityInference(
|
|
| 38 |
# stable-diffusion-512-v2-1 stable-diffusion-768-v2-1 stable-inpainting-v1-0 stable-inpainting-512-v2-0
|
| 39 |
)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def search_internet(question):
|
| 42 |
try:
|
| 43 |
params = {
|
|
@@ -63,7 +74,7 @@ def search_internet(question):
|
|
| 63 |
search = GoogleSearch(params)
|
| 64 |
results = search.get_dict()
|
| 65 |
organic_results = results["organic_results"]
|
| 66 |
-
|
| 67 |
|
| 68 |
snippets = ""
|
| 69 |
counter = 1
|
|
@@ -81,8 +92,11 @@ def search_internet(question):
|
|
| 81 |
top_p=1,
|
| 82 |
frequency_penalty=0,
|
| 83 |
presence_penalty=0)
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
string_temp = response.choices[0].text
|
|
|
|
| 86 |
st.write(string_temp)
|
| 87 |
st.write(snippets)
|
| 88 |
except:
|
|
@@ -128,8 +142,11 @@ def search_internet(question):
|
|
| 128 |
top_p=1,
|
| 129 |
frequency_penalty=0,
|
| 130 |
presence_penalty=0)
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
string_temp = response.choices[0].text
|
|
|
|
| 133 |
st.write(string_temp)
|
| 134 |
st.write(snippets)
|
| 135 |
except:
|
|
@@ -174,48 +191,18 @@ def search_internet(question):
|
|
| 174 |
top_p=1,
|
| 175 |
frequency_penalty=0,
|
| 176 |
presence_penalty=0)
|
| 177 |
-
|
|
|
|
|
|
|
| 178 |
string_temp = response.choices[0].text
|
|
|
|
| 179 |
st.write(string_temp)
|
| 180 |
st.write(snippets)
|
| 181 |
|
| 182 |
|
| 183 |
-
# openai.api_key = ""
|
| 184 |
openai.api_key = st.secrets["OPENAI_KEY"] #os.environ("OPENAI_KEY") #os.environ['OPENAI_KEY']
|
| 185 |
date_time = str(datetime.now())
|
| 186 |
|
| 187 |
-
# dictionary = st.secrets("GSHEET_KEY")
|
| 188 |
-
# json_object = json.dumps(dictionary, indent=4)
|
| 189 |
-
|
| 190 |
-
# def g_sheet_log(myinput, output):
|
| 191 |
-
# SERVICE_ACCOUNT_FILE = 'gsheet.json'
|
| 192 |
-
|
| 193 |
-
# credentials = service_account.Credentials.from_service_account_file(
|
| 194 |
-
# filename=SERVICE_ACCOUNT_FILE
|
| 195 |
-
# )
|
| 196 |
-
|
| 197 |
-
# service_sheets = build('sheets', 'v4', credentials=credentials)
|
| 198 |
-
|
| 199 |
-
# GOOGLE_SHEETS_ID = '16cM8lHm7n_X0ZVLgWfL5fcBhvKWIGO9LQz3zCl2Dn_8'
|
| 200 |
-
# worksheet_name = 'Prompt_Logs!'
|
| 201 |
-
# cell_range_insert = 'A:C'
|
| 202 |
-
|
| 203 |
-
# values = (
|
| 204 |
-
# (myinput, output, date_time),
|
| 205 |
-
# )
|
| 206 |
-
|
| 207 |
-
# value_range_body = {
|
| 208 |
-
# 'majorDimension' : 'ROWS',
|
| 209 |
-
# 'values' : values
|
| 210 |
-
# }
|
| 211 |
-
|
| 212 |
-
# service_sheets.spreadsheets().values().append(
|
| 213 |
-
# spreadsheetId=GOOGLE_SHEETS_ID,
|
| 214 |
-
# valueInputOption='USER_ENTERED',
|
| 215 |
-
# range=worksheet_name + cell_range_insert,
|
| 216 |
-
# body=value_range_body
|
| 217 |
-
# ).execute()
|
| 218 |
-
|
| 219 |
openai.api_key = st.secrets["OPENAI_KEY"]
|
| 220 |
|
| 221 |
def openai_response(PROMPT):
|
|
@@ -255,15 +242,14 @@ Input_type = st.radio(
|
|
| 255 |
)
|
| 256 |
|
| 257 |
if Input_type == 'TEXT':
|
| 258 |
-
# st.write('**You are now in Text input mode**')
|
| 259 |
mytext = st.text_input('**Go on! Ask me anything:**')
|
| 260 |
if st.button("SUBMIT"):
|
| 261 |
question=mytext
|
| 262 |
response = openai.Completion.create(
|
| 263 |
model="text-davinci-003",
|
| 264 |
prompt=f'''Your name is HyperBot and knowledge cutoff date is 2021-09, and you are not aware of any events after that time. if the
|
| 265 |
-
Answer to following questions is not from your knowledge base or in case of queries like weather
|
| 266 |
-
updates / stock updates / current news or people which requires you to have internet connection then print i don't have access to internet to answer your question,
|
| 267 |
if question is related to image or painting or drawing generation then print ipython type output function gen_draw("detailed prompt of image to be generated")
|
| 268 |
if the question is related to playing a song or video or music of a singer then print ipython type output function vid_tube("relevent search query")
|
| 269 |
if the question is related to operating home appliances then print ipython type output function home_app(" action(ON/Off),appliance(TV,Geaser,Fridge,Lights,fans,AC)") .
|
|
@@ -285,13 +271,17 @@ if Input_type == 'TEXT':
|
|
| 285 |
img2 = Image.open(wget.download(openai_response(prompt)))
|
| 286 |
img2.show()
|
| 287 |
rx = 'Image returned'
|
| 288 |
-
|
|
|
|
|
|
|
| 289 |
except:
|
| 290 |
urllib.request.urlretrieve(openai_response(prompt),"img_ret.png")
|
| 291 |
img = Image.open("img_ret.png")
|
| 292 |
img.show()
|
| 293 |
rx = 'Image returned'
|
| 294 |
-
|
|
|
|
|
|
|
| 295 |
except:
|
| 296 |
# Set up our initial generation parameters.
|
| 297 |
answers = stability_api.generate(
|
|
@@ -311,8 +301,6 @@ if Input_type == 'TEXT':
|
|
| 311 |
# (Available Samplers: ddim, plms, k_euler, k_euler_ancestral, k_heun, k_dpm_2, k_dpm_2_ancestral, k_dpmpp_2s_ancestral, k_lms, k_dpmpp_2m)
|
| 312 |
)
|
| 313 |
|
| 314 |
-
# Set up our warning to print to the console if the adult content classifier is tripped.
|
| 315 |
-
# If adult content classifier is not tripped, save generated images.
|
| 316 |
for resp in answers:
|
| 317 |
for artifact in resp.artifacts:
|
| 318 |
if artifact.finish_reason == generation.FILTER:
|
|
@@ -325,26 +313,8 @@ if Input_type == 'TEXT':
|
|
| 325 |
img.save(str(artifact.seed)+ ".png") # Save our generated images with their seed number as the filename.
|
| 326 |
rx = 'Image returned'
|
| 327 |
# g_sheet_log(mytext, rx)
|
|
|
|
| 328 |
|
| 329 |
-
# except:
|
| 330 |
-
# st.write('image is being generated please wait...')
|
| 331 |
-
# def extract_image_description(input_string):
|
| 332 |
-
# return input_string.split('gen_draw("')[1].split('")')[0]
|
| 333 |
-
# prompt=extract_image_description(string_temp)
|
| 334 |
-
# # model_id = "CompVis/stable-diffusion-v1-4"
|
| 335 |
-
# model_id='runwayml/stable-diffusion-v1-5'
|
| 336 |
-
# device = "cuda"
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
# pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 340 |
-
# pipe = pipe.to(device)
|
| 341 |
-
|
| 342 |
-
# # prompt = "a photo of an astronaut riding a horse on mars"
|
| 343 |
-
# image = pipe(prompt).images[0]
|
| 344 |
-
|
| 345 |
-
# image.save("astronaut_rides_horse.png")
|
| 346 |
-
# st.image(image)
|
| 347 |
-
# # image
|
| 348 |
|
| 349 |
elif ("vid_tube" in string_temp):
|
| 350 |
s = Search(mytext)
|
|
@@ -358,19 +328,26 @@ if Input_type == 'TEXT':
|
|
| 358 |
OurURL = YoutubeURL + video_id
|
| 359 |
st.write(OurURL)
|
| 360 |
st_player(OurURL)
|
|
|
|
|
|
|
| 361 |
ry = 'Youtube link and video returned'
|
| 362 |
# g_sheet_log(mytext, ry)
|
|
|
|
| 363 |
|
| 364 |
elif ("don't" in string_temp or "internet" in string_temp):
|
| 365 |
st.write('searching internet ')
|
| 366 |
search_internet(question)
|
| 367 |
-
rz = 'Internet result returned'
|
| 368 |
# g_sheet_log(mytext, string_temp)
|
|
|
|
| 369 |
|
| 370 |
else:
|
| 371 |
st.write(string_temp)
|
| 372 |
# g_sheet_log(mytext, string_temp)
|
| 373 |
-
|
|
|
|
|
|
|
|
|
|
| 374 |
elif Input_type == 'SPEECH':
|
| 375 |
option_speech = st.selectbox(
|
| 376 |
'Choose from below: (Options for Transcription)',
|
|
@@ -458,12 +435,22 @@ elif Input_type == 'SPEECH':
|
|
| 458 |
OurURL = YoutubeURL + video_id
|
| 459 |
st.write(OurURL)
|
| 460 |
st_player(OurURL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
|
| 462 |
elif ("don't" in string_temp or "internet" in string_temp ):
|
| 463 |
st.write('*searching internet*')
|
| 464 |
search_internet(question)
|
| 465 |
else:
|
| 466 |
st.write(string_temp)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
|
| 468 |
elif option_speech == 'OpenAI Whisper (Upload audio file)':
|
| 469 |
audio_file = st.file_uploader("Upload Audio file",type=['wav', 'mp3'])
|
|
@@ -523,11 +510,21 @@ elif Input_type == 'SPEECH':
|
|
| 523 |
OurURL = YoutubeURL + video_id
|
| 524 |
st.write(OurURL)
|
| 525 |
st_player(OurURL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 526 |
|
| 527 |
elif ("don't" in string_temp or "internet" in string_temp ):
|
| 528 |
st.write('*searching internet*')
|
| 529 |
search_internet(question)
|
| 530 |
else:
|
| 531 |
st.write(string_temp)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
else:
|
| 533 |
pass
|
|
|
|
| 24 |
from googleapiclient.discovery import build
|
| 25 |
import wget
|
| 26 |
import urllib.request
|
| 27 |
+
import csv
|
| 28 |
|
| 29 |
|
| 30 |
def save_uploadedfile(uploadedfile):
|
|
|
|
| 39 |
# stable-diffusion-512-v2-1 stable-diffusion-768-v2-1 stable-inpainting-v1-0 stable-inpainting-512-v2-0
|
| 40 |
)
|
| 41 |
|
| 42 |
+
header = ["sl. no.", "Input Prompt", "Output", "Date_time"]
|
| 43 |
+
|
| 44 |
+
def csv_logs(mytext, result, date_time):
|
| 45 |
+
with open("logs.csv", "r") as file:
|
| 46 |
+
sl_no = sum(1 for _ in csv.reader(file))
|
| 47 |
+
|
| 48 |
+
with open("logs.csv", "a", newline="") as file:
|
| 49 |
+
writer = csv.writer(file)
|
| 50 |
+
writer.writerow([sl_no, mytext, result, date_time])
|
| 51 |
+
|
| 52 |
def search_internet(question):
|
| 53 |
try:
|
| 54 |
params = {
|
|
|
|
| 74 |
search = GoogleSearch(params)
|
| 75 |
results = search.get_dict()
|
| 76 |
organic_results = results["organic_results"]
|
| 77 |
+
|
| 78 |
|
| 79 |
snippets = ""
|
| 80 |
counter = 1
|
|
|
|
| 92 |
top_p=1,
|
| 93 |
frequency_penalty=0,
|
| 94 |
presence_penalty=0)
|
| 95 |
+
|
| 96 |
+
now = datetime.datetime.now()
|
| 97 |
+
date_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 98 |
string_temp = response.choices[0].text
|
| 99 |
+
csv_logs(question, string_temp, date_time)
|
| 100 |
st.write(string_temp)
|
| 101 |
st.write(snippets)
|
| 102 |
except:
|
|
|
|
| 142 |
top_p=1,
|
| 143 |
frequency_penalty=0,
|
| 144 |
presence_penalty=0)
|
| 145 |
+
|
| 146 |
+
now = datetime.datetime.now()
|
| 147 |
+
date_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 148 |
string_temp = response.choices[0].text
|
| 149 |
+
csv_logs(question, string_temp, date_time)
|
| 150 |
st.write(string_temp)
|
| 151 |
st.write(snippets)
|
| 152 |
except:
|
|
|
|
| 191 |
top_p=1,
|
| 192 |
frequency_penalty=0,
|
| 193 |
presence_penalty=0)
|
| 194 |
+
|
| 195 |
+
now = datetime.datetime.now()
|
| 196 |
+
date_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 197 |
string_temp = response.choices[0].text
|
| 198 |
+
csv_logs(question, string_temp, date_time)
|
| 199 |
st.write(string_temp)
|
| 200 |
st.write(snippets)
|
| 201 |
|
| 202 |
|
|
|
|
| 203 |
openai.api_key = st.secrets["OPENAI_KEY"] #os.environ("OPENAI_KEY") #os.environ['OPENAI_KEY']
|
| 204 |
date_time = str(datetime.now())
|
| 205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
openai.api_key = st.secrets["OPENAI_KEY"]
|
| 207 |
|
| 208 |
def openai_response(PROMPT):
|
|
|
|
| 242 |
)
|
| 243 |
|
| 244 |
if Input_type == 'TEXT':
|
|
|
|
| 245 |
mytext = st.text_input('**Go on! Ask me anything:**')
|
| 246 |
if st.button("SUBMIT"):
|
| 247 |
question=mytext
|
| 248 |
response = openai.Completion.create(
|
| 249 |
model="text-davinci-003",
|
| 250 |
prompt=f'''Your name is HyperBot and knowledge cutoff date is 2021-09, and you are not aware of any events after that time. if the
|
| 251 |
+
Answer to following questions is not from your knowledge base or in case of queries like date, time, weather
|
| 252 |
+
updates / stock updates / current affairs / news or people which requires you to have internet connection then print i don't have access to internet to answer your question,
|
| 253 |
if question is related to image or painting or drawing generation then print ipython type output function gen_draw("detailed prompt of image to be generated")
|
| 254 |
if the question is related to playing a song or video or music of a singer then print ipython type output function vid_tube("relevent search query")
|
| 255 |
if the question is related to operating home appliances then print ipython type output function home_app(" action(ON/Off),appliance(TV,Geaser,Fridge,Lights,fans,AC)") .
|
|
|
|
| 271 |
img2 = Image.open(wget.download(openai_response(prompt)))
|
| 272 |
img2.show()
|
| 273 |
rx = 'Image returned'
|
| 274 |
+
now = datetime.datetime.now()
|
| 275 |
+
date_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 276 |
+
csv_logs(mytext, rx, date_time)
|
| 277 |
except:
|
| 278 |
urllib.request.urlretrieve(openai_response(prompt),"img_ret.png")
|
| 279 |
img = Image.open("img_ret.png")
|
| 280 |
img.show()
|
| 281 |
rx = 'Image returned'
|
| 282 |
+
now = datetime.datetime.now()
|
| 283 |
+
date_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 284 |
+
csv_logs(mytext, rx, date_time)
|
| 285 |
except:
|
| 286 |
# Set up our initial generation parameters.
|
| 287 |
answers = stability_api.generate(
|
|
|
|
| 301 |
# (Available Samplers: ddim, plms, k_euler, k_euler_ancestral, k_heun, k_dpm_2, k_dpm_2_ancestral, k_dpmpp_2s_ancestral, k_lms, k_dpmpp_2m)
|
| 302 |
)
|
| 303 |
|
|
|
|
|
|
|
| 304 |
for resp in answers:
|
| 305 |
for artifact in resp.artifacts:
|
| 306 |
if artifact.finish_reason == generation.FILTER:
|
|
|
|
| 313 |
img.save(str(artifact.seed)+ ".png") # Save our generated images with their seed number as the filename.
|
| 314 |
rx = 'Image returned'
|
| 315 |
# g_sheet_log(mytext, rx)
|
| 316 |
+
csv_logs(mytext, rx, date_time)
|
| 317 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
elif ("vid_tube" in string_temp):
|
| 320 |
s = Search(mytext)
|
|
|
|
| 328 |
OurURL = YoutubeURL + video_id
|
| 329 |
st.write(OurURL)
|
| 330 |
st_player(OurURL)
|
| 331 |
+
now = datetime.datetime.now()
|
| 332 |
+
date_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 333 |
ry = 'Youtube link and video returned'
|
| 334 |
# g_sheet_log(mytext, ry)
|
| 335 |
+
csv_logs(mytext, ry, date_time)
|
| 336 |
|
| 337 |
elif ("don't" in string_temp or "internet" in string_temp):
|
| 338 |
st.write('searching internet ')
|
| 339 |
search_internet(question)
|
| 340 |
+
# rz = 'Internet result returned'
|
| 341 |
# g_sheet_log(mytext, string_temp)
|
| 342 |
+
# csv_logs(mytext, rz, date_time)
|
| 343 |
|
| 344 |
else:
|
| 345 |
st.write(string_temp)
|
| 346 |
# g_sheet_log(mytext, string_temp)
|
| 347 |
+
now = datetime.datetime.now()
|
| 348 |
+
date_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 349 |
+
csv_logs(mytext, string_temp, date_time)
|
| 350 |
+
|
| 351 |
elif Input_type == 'SPEECH':
|
| 352 |
option_speech = st.selectbox(
|
| 353 |
'Choose from below: (Options for Transcription)',
|
|
|
|
| 435 |
OurURL = YoutubeURL + video_id
|
| 436 |
st.write(OurURL)
|
| 437 |
st_player(OurURL)
|
| 438 |
+
now = datetime.datetime.now()
|
| 439 |
+
date_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 440 |
+
ry = 'Youtube link and video returned'
|
| 441 |
+
# g_sheet_log(mytext, ry)
|
| 442 |
+
csv_logs(mytext, ry, date_time)
|
| 443 |
+
|
| 444 |
|
| 445 |
elif ("don't" in string_temp or "internet" in string_temp ):
|
| 446 |
st.write('*searching internet*')
|
| 447 |
search_internet(question)
|
| 448 |
else:
|
| 449 |
st.write(string_temp)
|
| 450 |
+
now = datetime.datetime.now()
|
| 451 |
+
date_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 452 |
+
csv_logs(mytext, string_temp, date_time)
|
| 453 |
+
|
| 454 |
|
| 455 |
elif option_speech == 'OpenAI Whisper (Upload audio file)':
|
| 456 |
audio_file = st.file_uploader("Upload Audio file",type=['wav', 'mp3'])
|
|
|
|
| 510 |
OurURL = YoutubeURL + video_id
|
| 511 |
st.write(OurURL)
|
| 512 |
st_player(OurURL)
|
| 513 |
+
now = datetime.datetime.now()
|
| 514 |
+
date_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 515 |
+
ry = 'Youtube link and video returned'
|
| 516 |
+
# g_sheet_log(mytext, ry)
|
| 517 |
+
csv_logs(mytext, ry, date_time)
|
| 518 |
|
| 519 |
elif ("don't" in string_temp or "internet" in string_temp ):
|
| 520 |
st.write('*searching internet*')
|
| 521 |
search_internet(question)
|
| 522 |
else:
|
| 523 |
st.write(string_temp)
|
| 524 |
+
# g_sheet_log(mytext, ry)
|
| 525 |
+
now = datetime.datetime.now()
|
| 526 |
+
date_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 527 |
+
csv_logs(mytext, string_temp, date_time)
|
| 528 |
+
|
| 529 |
else:
|
| 530 |
pass
|