Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,6 +26,9 @@ import urllib.request
|
|
| 26 |
import sqlite3
|
| 27 |
import pandas as pd
|
| 28 |
import pandasql as ps
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def clean(value):
|
| 31 |
val = value.replace("'",'').replace("[",'').replace("]",'')
|
|
@@ -249,6 +252,45 @@ def g_sheet_log(myinput, output):
|
|
| 249 |
body=value_range_body
|
| 250 |
).execute()
|
| 251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
def openai_response(PROMPT):
|
| 254 |
response = openai.Image.create(
|
|
@@ -446,223 +488,284 @@ elif Usage == 'Random Questions':
|
|
| 446 |
6. Current Affairs and News.
|
| 447 |
7. Create or compose tweets or Linkedin posts or email.''')
|
| 448 |
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
# )
|
| 453 |
-
|
| 454 |
-
# if Input_type == 'TEXT':
|
| 455 |
-
#page_bg_img2 = """
|
| 456 |
-
#<style>
|
| 457 |
-
#[data-testid="stAppViewContainer"] {
|
| 458 |
-
#background-color: #e5e5f7;
|
| 459 |
-
#opacity: 0.8;
|
| 460 |
-
#background-size: 20px 20px;
|
| 461 |
-
#background-image: repeating-linear-gradient(0deg, #32d947, #32d947 1px, #e5e5f7 1px, #e5e5f7);
|
| 462 |
-
#}
|
| 463 |
-
#</style>
|
| 464 |
-
#"""
|
| 465 |
-
#st.markdown(page_bg_img, unsafe_allow_html=True)
|
| 466 |
-
st.write('**You are now in Text input mode**')
|
| 467 |
-
mytext = st.text_input('**Go on! Ask me anything:**')
|
| 468 |
-
if st.button("SUBMIT"):
|
| 469 |
-
question=mytext
|
| 470 |
-
response = openai.Completion.create(
|
| 471 |
-
model="text-davinci-003",
|
| 472 |
-
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
|
| 473 |
-
Answer to following questions is not from your knowledge base or in case of queries like weather
|
| 474 |
-
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,
|
| 475 |
-
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")
|
| 476 |
-
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")
|
| 477 |
-
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)") .
|
| 478 |
-
if question is realted to sending mail or sms then print ipython type output function messenger_app(" message of us ,messenger(email,sms)")
|
| 479 |
-
\nQuestion-{question}
|
| 480 |
-
\nAnswer -''',
|
| 481 |
-
temperature=0.49,
|
| 482 |
-
max_tokens=256,
|
| 483 |
-
top_p=1,
|
| 484 |
-
frequency_penalty=0,
|
| 485 |
-
presence_penalty=0
|
| 486 |
)
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
# Note: This isn't quite the case for Clip Guided generations, which we'll tackle in a future example notebook.
|
| 510 |
-
steps=30, # Amount of inference steps performed on image generation. Defaults to 30.
|
| 511 |
-
cfg_scale=8.0, # Influences how strongly your generation is guided to match your prompt.
|
| 512 |
-
# Setting this value higher increases the strength in which it tries to match your prompt.
|
| 513 |
-
# Defaults to 7.0 if not specified.
|
| 514 |
-
width=512, # Generation width, defaults to 512 if not included.
|
| 515 |
-
height=512, # Generation height, defaults to 512 if not included.
|
| 516 |
-
samples=1, # Number of images to generate, defaults to 1 if not included.
|
| 517 |
-
sampler=generation.SAMPLER_K_DPMPP_2M # Choose which sampler we want to denoise our generation with.
|
| 518 |
-
# Defaults to k_dpmpp_2m if not specified. Clip Guidance only supports ancestral samplers.
|
| 519 |
-
# (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)
|
| 520 |
)
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
"Please modify the prompt and try again.")
|
| 530 |
-
if artifact.type == generation.ARTIFACT_IMAGE:
|
| 531 |
-
img = Image.open(io.BytesIO(artifact.binary))
|
| 532 |
-
st.image(img)
|
| 533 |
-
img.save(str(artifact.seed)+ ".png") # Save our generated images with their seed number as the filename.
|
| 534 |
rx = 'Image returned'
|
| 535 |
g_sheet_log(mytext, rx)
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
g_sheet_log(mytext, string_temp)
|
| 577 |
-
|
| 578 |
-
else:
|
| 579 |
-
st.write(string_temp)
|
| 580 |
-
g_sheet_log(mytext, string_temp)
|
| 581 |
-
else:
|
| 582 |
-
pass
|
| 583 |
-
# elif Input_type == 'SPEECH':
|
| 584 |
-
# stt_button = Button(label="Speak", width=100)
|
| 585 |
-
# stt_button.js_on_event("button_click", CustomJS(code="""
|
| 586 |
-
# var recognition = new webkitSpeechRecognition();
|
| 587 |
-
# recognition.continuous = true;
|
| 588 |
-
# recognition.interimResults = true;
|
| 589 |
-
# recognition.onresult = function (e) {
|
| 590 |
-
# var value = "";
|
| 591 |
-
# for (var i = e.resultIndex; i < e.results.length; ++i) {
|
| 592 |
-
# if (e.results[i].isFinal) {
|
| 593 |
-
# value += e.results[i][0].transcript;
|
| 594 |
-
# }
|
| 595 |
-
# }
|
| 596 |
-
# if ( value != "") {
|
| 597 |
-
# document.dispatchEvent(new CustomEvent("GET_TEXT", {detail: value}));
|
| 598 |
-
# }
|
| 599 |
-
# }
|
| 600 |
-
# recognition.start();
|
| 601 |
-
# """))
|
| 602 |
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
import sqlite3
|
| 27 |
import pandas as pd
|
| 28 |
import pandasql as ps
|
| 29 |
+
import pyaudio
|
| 30 |
+
import wave
|
| 31 |
+
|
| 32 |
|
| 33 |
def clean(value):
|
| 34 |
val = value.replace("'",'').replace("[",'').replace("]",'')
|
|
|
|
| 252 |
body=value_range_body
|
| 253 |
).execute()
|
| 254 |
|
| 255 |
+
openai.api_key = st.secrets["OPENAI_KEY"]
|
| 256 |
+
RATE = 44100
|
| 257 |
+
CHANNELS = 1
|
| 258 |
+
FORMAT = pyaudio.paInt16
|
| 259 |
+
CHUNK = 1024
|
| 260 |
+
RECORD_SECONDS = 5
|
| 261 |
+
|
| 262 |
+
def record_audio():
|
| 263 |
+
p = pyaudio.PyAudio()
|
| 264 |
+
|
| 265 |
+
# Open the microphone stream
|
| 266 |
+
stream = p.open(format=FORMAT,
|
| 267 |
+
channels=CHANNELS,
|
| 268 |
+
rate=RATE,
|
| 269 |
+
input=True,
|
| 270 |
+
frames_per_buffer=CHUNK)
|
| 271 |
+
|
| 272 |
+
# Record the audio
|
| 273 |
+
frames = []
|
| 274 |
+
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
|
| 275 |
+
data = stream.read(CHUNK)
|
| 276 |
+
frames.append(data)
|
| 277 |
+
|
| 278 |
+
# Close the microphone stream
|
| 279 |
+
stream.stop_stream()
|
| 280 |
+
stream.close()
|
| 281 |
+
p.terminate()
|
| 282 |
+
|
| 283 |
+
# Save the recorded audio to a WAV file
|
| 284 |
+
wf = wave.open("output.mp3", "wb")
|
| 285 |
+
wf.setnchannels(CHANNELS)
|
| 286 |
+
wf.setsampwidth(p.get_sample_size(FORMAT))
|
| 287 |
+
wf.setframerate(RATE)
|
| 288 |
+
wf.writeframes(b"".join(frames))
|
| 289 |
+
wf.close()
|
| 290 |
+
|
| 291 |
+
# Return the path to the recorded audio file
|
| 292 |
+
return "output.mp3"
|
| 293 |
+
|
| 294 |
|
| 295 |
def openai_response(PROMPT):
|
| 296 |
response = openai.Image.create(
|
|
|
|
| 488 |
6. Current Affairs and News.
|
| 489 |
7. Create or compose tweets or Linkedin posts or email.''')
|
| 490 |
|
| 491 |
+
Input_type = st.radio(
|
| 492 |
+
"**Input type:**",
|
| 493 |
+
('TEXT', 'SPEECH')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 494 |
)
|
| 495 |
+
|
| 496 |
+
if Input_type == 'TEXT':
|
| 497 |
+
st.write('**You are now in Text input mode**')
|
| 498 |
+
mytext = st.text_input('**Go on! Ask me anything:**')
|
| 499 |
+
if st.button("SUBMIT"):
|
| 500 |
+
question=mytext
|
| 501 |
+
response = openai.Completion.create(
|
| 502 |
+
model="text-davinci-003",
|
| 503 |
+
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
|
| 504 |
+
Answer to following questions is not from your knowledge base or in case of queries like weather
|
| 505 |
+
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,
|
| 506 |
+
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")
|
| 507 |
+
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")
|
| 508 |
+
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)") .
|
| 509 |
+
if question is realted to sending mail or sms then print ipython type output function messenger_app(" message of us ,messenger(email,sms)")
|
| 510 |
+
\nQuestion-{question}
|
| 511 |
+
\nAnswer -''',
|
| 512 |
+
temperature=0.49,
|
| 513 |
+
max_tokens=256,
|
| 514 |
+
top_p=1,
|
| 515 |
+
frequency_penalty=0,
|
| 516 |
+
presence_penalty=0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 517 |
)
|
| 518 |
+
string_temp=response.choices[0].text
|
| 519 |
+
|
| 520 |
+
if ("gen_draw" in string_temp):
|
| 521 |
+
try:
|
| 522 |
+
try:
|
| 523 |
+
wget.download(openai_response(prompt))
|
| 524 |
+
img2 = Image.open(wget.download(openai_response(prompt)))
|
| 525 |
+
img2.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 526 |
rx = 'Image returned'
|
| 527 |
g_sheet_log(mytext, rx)
|
| 528 |
+
except:
|
| 529 |
+
urllib.request.urlretrieve(openai_response(prompt),"img_ret.png")
|
| 530 |
+
img = Image.open("img_ret.png")
|
| 531 |
+
img.show()
|
| 532 |
+
rx = 'Image returned'
|
| 533 |
+
g_sheet_log(mytext, rx)
|
| 534 |
+
except:
|
| 535 |
+
# Set up our initial generation parameters.
|
| 536 |
+
answers = stability_api.generate(
|
| 537 |
+
prompt = mytext,
|
| 538 |
+
seed=992446758, # If a seed is provided, the resulting generated image will be deterministic.
|
| 539 |
+
# What this means is that as long as all generation parameters remain the same, you can always recall the same image simply by generating it again.
|
| 540 |
+
# Note: This isn't quite the case for Clip Guided generations, which we'll tackle in a future example notebook.
|
| 541 |
+
steps=30, # Amount of inference steps performed on image generation. Defaults to 30.
|
| 542 |
+
cfg_scale=8.0, # Influences how strongly your generation is guided to match your prompt.
|
| 543 |
+
# Setting this value higher increases the strength in which it tries to match your prompt.
|
| 544 |
+
# Defaults to 7.0 if not specified.
|
| 545 |
+
width=512, # Generation width, defaults to 512 if not included.
|
| 546 |
+
height=512, # Generation height, defaults to 512 if not included.
|
| 547 |
+
samples=1, # Number of images to generate, defaults to 1 if not included.
|
| 548 |
+
sampler=generation.SAMPLER_K_DPMPP_2M # Choose which sampler we want to denoise our generation with.
|
| 549 |
+
# Defaults to k_dpmpp_2m if not specified. Clip Guidance only supports ancestral samplers.
|
| 550 |
+
# (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)
|
| 551 |
+
)
|
| 552 |
+
|
| 553 |
+
# Set up our warning to print to the console if the adult content classifier is tripped.
|
| 554 |
+
# If adult content classifier is not tripped, save generated images.
|
| 555 |
+
for resp in answers:
|
| 556 |
+
for artifact in resp.artifacts:
|
| 557 |
+
if artifact.finish_reason == generation.FILTER:
|
| 558 |
+
warnings.warn(
|
| 559 |
+
"Your request activated the API's safety filters and could not be processed."
|
| 560 |
+
"Please modify the prompt and try again.")
|
| 561 |
+
if artifact.type == generation.ARTIFACT_IMAGE:
|
| 562 |
+
img = Image.open(io.BytesIO(artifact.binary))
|
| 563 |
+
st.image(img)
|
| 564 |
+
img.save(str(artifact.seed)+ ".png") # Save our generated images with their seed number as the filename.
|
| 565 |
+
rx = 'Image returned'
|
| 566 |
+
g_sheet_log(mytext, rx)
|
| 567 |
+
|
| 568 |
+
# except:
|
| 569 |
+
# st.write('image is being generated please wait...')
|
| 570 |
+
# def extract_image_description(input_string):
|
| 571 |
+
# return input_string.split('gen_draw("')[1].split('")')[0]
|
| 572 |
+
# prompt=extract_image_description(string_temp)
|
| 573 |
+
# # model_id = "CompVis/stable-diffusion-v1-4"
|
| 574 |
+
# model_id='runwayml/stable-diffusion-v1-5'
|
| 575 |
+
# device = "cuda"
|
| 576 |
+
|
| 577 |
+
|
| 578 |
+
# pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 579 |
+
# pipe = pipe.to(device)
|
| 580 |
+
|
| 581 |
+
# # prompt = "a photo of an astronaut riding a horse on mars"
|
| 582 |
+
# image = pipe(prompt).images[0]
|
| 583 |
+
|
| 584 |
+
# image.save("astronaut_rides_horse.png")
|
| 585 |
+
# st.image(image)
|
| 586 |
+
# # image
|
| 587 |
+
|
| 588 |
+
elif ("vid_tube" in string_temp):
|
| 589 |
+
s = Search(mytext)
|
| 590 |
+
search_res = s.results
|
| 591 |
+
first_vid = search_res[0]
|
| 592 |
+
print(first_vid)
|
| 593 |
+
string = str(first_vid)
|
| 594 |
+
video_id = string[string.index('=') + 1:-1]
|
| 595 |
+
# print(video_id)
|
| 596 |
+
YoutubeURL = "https://www.youtube.com/watch?v="
|
| 597 |
+
OurURL = YoutubeURL + video_id
|
| 598 |
+
st.write(OurURL)
|
| 599 |
+
st_player(OurURL)
|
| 600 |
+
ry = 'Youtube link and video returned'
|
| 601 |
+
g_sheet_log(mytext, ry)
|
| 602 |
+
|
| 603 |
+
elif ("don't" in string_temp or "internet" in string_temp):
|
| 604 |
+
st.write('searching internet ')
|
| 605 |
+
search_internet(question)
|
| 606 |
+
rz = 'Internet result returned'
|
| 607 |
+
g_sheet_log(mytext, string_temp)
|
| 608 |
+
|
| 609 |
+
else:
|
| 610 |
+
st.write(string_temp)
|
| 611 |
+
g_sheet_log(mytext, string_temp)
|
| 612 |
+
|
| 613 |
+
elif Input_type == 'SPEECH':
|
| 614 |
+
try:
|
| 615 |
+
st.text("Record your audio, **max length - 5 seconds**")
|
| 616 |
+
if st.button("Record"):
|
| 617 |
+
st.write("Recording...")
|
| 618 |
+
audio_file = record_audio()
|
| 619 |
+
st.write("Recording complete.")
|
| 620 |
+
file = open(audio_file, "rb")
|
| 621 |
|
| 622 |
+
# Play the recorded audio
|
| 623 |
+
st.audio(audio_file)
|
| 624 |
+
|
| 625 |
+
transcription = openai.Audio.transcribe("whisper-1", file)
|
| 626 |
+
result = transcription["text"]
|
| 627 |
+
st.write(f"Fetched from audio - {result}")
|
| 628 |
+
question = result
|
| 629 |
+
response = openai.Completion.create(
|
| 630 |
+
model="text-davinci-003",
|
| 631 |
+
prompt=f'''Your knowledge cutoff is 2021-09, and it is not aware of any events after that time. if the
|
| 632 |
+
Answer to following questions is not from your knowledge base or in case of queries like weather
|
| 633 |
+
updates / stock updates / current news Etc which requires you to have internet connection then print i don't have access to internet to answer your question,
|
| 634 |
+
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")
|
| 635 |
+
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")
|
| 636 |
+
\nQuestion-{question}
|
| 637 |
+
\nAnswer -''',
|
| 638 |
+
temperature=0.49,
|
| 639 |
+
max_tokens=256,
|
| 640 |
+
top_p=1,
|
| 641 |
+
frequency_penalty=0,
|
| 642 |
+
presence_penalty=0
|
| 643 |
+
)
|
| 644 |
+
string_temp=response.choices[0].text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
|
| 646 |
+
if ("gen_draw" in string_temp):
|
| 647 |
+
st.write('*image is being generated please wait..* ')
|
| 648 |
+
def extract_image_description(input_string):
|
| 649 |
+
return input_string.split('gen_draw("')[1].split('")')[0]
|
| 650 |
+
prompt=extract_image_description(string_temp)
|
| 651 |
+
# model_id = "CompVis/stable-diffusion-v1-4"
|
| 652 |
+
model_id='runwayml/stable-diffusion-v1-5'
|
| 653 |
+
device = "cuda"
|
| 654 |
+
|
| 655 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 656 |
+
pipe = pipe.to(device)
|
| 657 |
+
|
| 658 |
+
# prompt = "a photo of an astronaut riding a horse on mars"
|
| 659 |
+
image = pipe(prompt).images[0]
|
| 660 |
+
|
| 661 |
+
image.save("astronaut_rides_horse.png")
|
| 662 |
+
st.image(image)
|
| 663 |
+
# image
|
| 664 |
+
|
| 665 |
+
elif ("vid_tube" in string_temp):
|
| 666 |
+
s = Search(question)
|
| 667 |
+
search_res = s.results
|
| 668 |
+
first_vid = search_res[0]
|
| 669 |
+
print(first_vid)
|
| 670 |
+
string = str(first_vid)
|
| 671 |
+
video_id = string[string.index('=') + 1:-1]
|
| 672 |
+
# print(video_id)
|
| 673 |
+
YoutubeURL = "https://www.youtube.com/watch?v="
|
| 674 |
+
OurURL = YoutubeURL + video_id
|
| 675 |
+
st.write(OurURL)
|
| 676 |
+
st_player(OurURL)
|
| 677 |
+
|
| 678 |
+
elif ("don't" in string_temp or "internet" in string_temp ):
|
| 679 |
+
st.write('*searching internet*')
|
| 680 |
+
search_internet(question)
|
| 681 |
+
else:
|
| 682 |
+
st.write(string_temp)
|
| 683 |
+
|
| 684 |
+
except:
|
| 685 |
+
stt_button = Button(label="Speak", width=100)
|
| 686 |
+
stt_button.js_on_event("button_click", CustomJS(code="""
|
| 687 |
+
var recognition = new webkitSpeechRecognition();
|
| 688 |
+
recognition.continuous = true;
|
| 689 |
+
recognition.interimResults = true;
|
| 690 |
+
recognition.onresult = function (e) {
|
| 691 |
+
var value = "";
|
| 692 |
+
for (var i = e.resultIndex; i < e.results.length; ++i) {
|
| 693 |
+
if (e.results[i].isFinal) {
|
| 694 |
+
value += e.results[i][0].transcript;
|
| 695 |
+
}
|
| 696 |
+
}
|
| 697 |
+
if ( value != "") {
|
| 698 |
+
document.dispatchEvent(new CustomEvent("GET_TEXT", {detail: value}));
|
| 699 |
+
}
|
| 700 |
+
}
|
| 701 |
+
recognition.start();
|
| 702 |
+
"""))
|
| 703 |
|
| 704 |
+
result = streamlit_bokeh_events(
|
| 705 |
+
stt_button,
|
| 706 |
+
events="GET_TEXT",
|
| 707 |
+
key="listen",
|
| 708 |
+
refresh_on_update=False,
|
| 709 |
+
override_height=75,
|
| 710 |
+
debounce_time=0)
|
| 711 |
+
|
| 712 |
+
if result:
|
| 713 |
+
if "GET_TEXT" in result:
|
| 714 |
+
st.write(result.get("GET_TEXT"))
|
| 715 |
+
question = result.get("GET_TEXT")
|
| 716 |
+
response = openai.Completion.create(
|
| 717 |
+
model="text-davinci-003",
|
| 718 |
+
prompt=f'''Your knowledge cutoff is 2021-09, and it is not aware of any events after that time. if the
|
| 719 |
+
Answer to following questions is not from your knowledge base or in case of queries like weather
|
| 720 |
+
updates / stock updates / current news Etc which requires you to have internet connection then print i don't have access to internet to answer your question,
|
| 721 |
+
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")
|
| 722 |
+
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")
|
| 723 |
+
\nQuestion-{question}
|
| 724 |
+
\nAnswer -''',
|
| 725 |
+
temperature=0.49,
|
| 726 |
+
max_tokens=256,
|
| 727 |
+
top_p=1,
|
| 728 |
+
frequency_penalty=0,
|
| 729 |
+
presence_penalty=0
|
| 730 |
+
)
|
| 731 |
+
string_temp=response.choices[0].text
|
| 732 |
+
|
| 733 |
+
if ("gen_draw" in string_temp):
|
| 734 |
+
st.write('*image is being generated please wait..* ')
|
| 735 |
+
def extract_image_description(input_string):
|
| 736 |
+
return input_string.split('gen_draw("')[1].split('")')[0]
|
| 737 |
+
prompt=extract_image_description(string_temp)
|
| 738 |
+
# model_id = "CompVis/stable-diffusion-v1-4"
|
| 739 |
+
model_id='runwayml/stable-diffusion-v1-5'
|
| 740 |
+
device = "cuda"
|
| 741 |
+
|
| 742 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 743 |
+
pipe = pipe.to(device)
|
| 744 |
+
|
| 745 |
+
# prompt = "a photo of an astronaut riding a horse on mars"
|
| 746 |
+
image = pipe(prompt).images[0]
|
| 747 |
+
|
| 748 |
+
image.save("astronaut_rides_horse.png")
|
| 749 |
+
st.image(image)
|
| 750 |
+
# image
|
| 751 |
+
|
| 752 |
+
elif ("vid_tube" in string_temp):
|
| 753 |
+
s = Search(question)
|
| 754 |
+
search_res = s.results
|
| 755 |
+
first_vid = search_res[0]
|
| 756 |
+
print(first_vid)
|
| 757 |
+
string = str(first_vid)
|
| 758 |
+
video_id = string[string.index('=') + 1:-1]
|
| 759 |
+
# print(video_id)
|
| 760 |
+
YoutubeURL = "https://www.youtube.com/watch?v="
|
| 761 |
+
OurURL = YoutubeURL + video_id
|
| 762 |
+
st.write(OurURL)
|
| 763 |
+
st_player(OurURL)
|
| 764 |
+
|
| 765 |
+
elif ("don't" in string_temp or "internet" in string_temp ):
|
| 766 |
+
st.write('*searching internet*')
|
| 767 |
+
search_internet(question)
|
| 768 |
+
else:
|
| 769 |
+
st.write(string_temp)
|
| 770 |
+
else:
|
| 771 |
+
pass
|