CobaltZvc commited on
Commit
b5f653e
1 Parent(s): 78a1477

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +211 -141
app.py CHANGED
@@ -253,16 +253,16 @@ def g_sheet_log(myinput, output):
253
  ).execute()
254
 
255
  openai.api_key = st.secrets["OPENAI_KEY"]
256
- duration = 5
257
- fs = 44100
258
- channels = 1
259
- filename = "output.wav"
260
-
261
- def record_audio():
262
- myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=channels)
263
- sd.wait()
264
- sf.write(filename, myrecording, fs)
265
- return filename
266
  # p = pyaudio.PyAudio()
267
 
268
  # # Open the microphone stream
@@ -326,6 +326,7 @@ all with the help of HyperBot! 🤖 ✨
326
 
327
  option_ = ['Random Questions','Questions based on custom CSV data']
328
  Usage = st.selectbox('Select an option:', option_)
 
329
  if Usage == 'Questions based on custom CSV data':
330
  st.text('''
331
  You can use your own custom csv files to test this feature or
@@ -372,13 +373,13 @@ if Usage == 'Questions based on custom CSV data':
372
  st.success('loaded')
373
  with col4:
374
  try:
375
- sqlOutput = st.text_area('SQL Query', value=gpt3(col_p))
376
  warning(sqlOutput)
377
  cars=pd.read_csv('cars.csv')
378
  result_tab2=ps.sqldf(sqlOutput)
379
  st.write(result_tab2)
380
  with open("fewshot_matplot.txt", "r") as file:
381
- text_plot = file.read()
382
 
383
  result_tab = result_tab2.reset_index(drop=True)
384
  result_tab_string = result_tab.to_string()
@@ -428,57 +429,58 @@ if Usage == 'Questions based on custom CSV data':
428
 
429
  with st.form(key='columns_in_form2'):
430
  col3, col4 = st.columns(2)
431
- with col3:
432
- userPrompt = st.text_area("Input Prompt",'Enter Natural Language Query')
433
- submitButton = st.form_submit_button(label = 'Submit')
434
- if submitButton:
 
 
 
 
 
 
 
435
  try:
436
- col_p ="Create SQL statement from instruction. "+ext+" " " (" + column +")." +" Request:" + userPrompt + "SQL statement:"
437
- result = gpt3(col_p)
438
- except:
439
- results = gpt3(userPrompt)
440
- st.success('loaded')
441
- with col4:
442
- try:
443
- sqlOutput = st.text_area('SQL Query', value=gpt3(col_p))
444
- warning(sqlOutput)
445
- cars=pd.read_csv('cars.csv')
446
- result_tab2=ps.sqldf(sqlOutput)
447
- st.write(result_tab2)
448
- with open("fewshot_matplot.txt", "r") as file:
449
- text_plot = file.read()
450
-
451
- result_tab = result_tab2.reset_index(drop=True)
452
- result_tab_string = result_tab.to_string()
453
- gr_prompt = text_plot + userPrompt + result_tab_string + "Plot graph for: "
454
-
455
- if len(gr_prompt) > 4097:
456
- st.write('OVERWHELMING DATA!!! You have given me more than 4097 tokens! ^_^')
457
- st.write('As of today, the NLP model text-davinci-003 that I run on takes in inputs that have less than 4097 tokens. Kindly retry ^_^')
458
-
459
- elif len(result_tab2.columns) < 2:
460
- st.write("I need more data to conduct analysis and provide visualizations for you... ^_^")
461
-
462
- else:
463
- st.success("Plotting...")
464
- response_graph = openai.Completion.create(
465
- engine="text-davinci-003",
466
- prompt = gr_prompt,
467
- max_tokens=1024,
468
- n=1,
469
- stop=None,
470
- temperature=0.5,
471
- )
472
 
473
- if response_graph['choices'][0]['text'] != "":
474
- print(response_graph['choices'][0]['text'])
475
- exec(response_graph['choices'][0]['text'])
 
 
 
 
 
 
 
476
 
 
 
 
477
  else:
478
- print('Retry! Graph could not be plotted *_*')
479
-
480
- except:
481
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
482
 
483
 
484
  elif Usage == 'Random Questions':
@@ -614,93 +616,161 @@ elif Usage == 'Random Questions':
614
  g_sheet_log(mytext, string_temp)
615
 
616
  elif Input_type == 'SPEECH':
617
- stt_button = Button(label="Speak", width=100)
618
- stt_button.js_on_event("button_click", CustomJS(code="""
619
- var recognition = new webkitSpeechRecognition();
620
- recognition.continuous = true;
621
- recognition.interimResults = true;
622
-
623
- recognition.onresult = function (e) {
624
- var value = "";
625
- for (var i = e.resultIndex; i < e.results.length; ++i) {
626
- if (e.results[i].isFinal) {
627
- value += e.results[i][0].transcript;
 
 
 
 
 
 
 
 
 
 
628
  }
629
  }
630
- if ( value != "") {
631
- document.dispatchEvent(new CustomEvent("GET_TEXT", {detail: value}));
632
- }
633
- }
634
- recognition.start();
635
- """))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
636
 
637
- result = streamlit_bokeh_events(
638
- stt_button,
639
- events="GET_TEXT",
640
- key="listen",
641
- refresh_on_update=False,
642
- override_height=75,
643
- debounce_time=0)
644
 
645
- if result:
646
- if "GET_TEXT" in result:
647
- question = result.get("GET_TEXT")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
648
  response = openai.Completion.create(
649
- model="text-davinci-003",
650
- prompt=f'''Your knowledge cutoff is 2021-09, and it is not aware of any events after that time. if the
651
- Answer to following questions is not from your knowledge base or in case of queries like weather
652
- 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,
653
- 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")
654
- 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")
655
- \nQuestion-{question}
656
- \nAnswer -''',
657
- temperature=0.49,
658
- max_tokens=256,
659
- top_p=1,
660
- frequency_penalty=0,
661
- presence_penalty=0
662
- )
663
- string_temp=response.choices[0].text
664
-
665
- if ("gen_draw" in string_temp):
666
- st.write('*image is being generated please wait..* ')
667
- def extract_image_description(input_string):
668
- return input_string.split('gen_draw("')[1].split('")')[0]
669
- prompt=extract_image_description(string_temp)
670
- # model_id = "CompVis/stable-diffusion-v1-4"
671
- model_id='runwayml/stable-diffusion-v1-5'
672
- device = "cuda"
673
-
674
- pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
675
- pipe = pipe.to(device)
676
-
677
- # prompt = "a photo of an astronaut riding a horse on mars"
678
- image = pipe(prompt).images[0]
679
-
680
- image.save("astronaut_rides_horse.png")
681
- st.image(image)
682
- # image
683
-
684
- elif ("vid_tube" in string_temp):
685
- s = Search(question)
686
- search_res = s.results
687
- first_vid = search_res[0]
688
- print(first_vid)
689
- string = str(first_vid)
690
- video_id = string[string.index('=') + 1:-1]
691
- # print(video_id)
692
- YoutubeURL = "https://www.youtube.com/watch?v="
693
- OurURL = YoutubeURL + video_id
694
- st.write(OurURL)
695
- st_player(OurURL)
696
-
697
- elif ("don't" in string_temp or "internet" in string_temp ):
698
- st.write('*searching internet*')
699
- search_internet(question)
700
- else:
701
- st.write(string_temp)
702
-
703
-
704
 
705
  # except:
706
  # pass
 
253
  ).execute()
254
 
255
  openai.api_key = st.secrets["OPENAI_KEY"]
256
+ # duration = 5
257
+ # fs = 44100
258
+ # channels = 1
259
+ # filename = "output.wav"
260
+
261
+ # def record_audio():
262
+ # myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=channels)
263
+ # sd.wait()
264
+ # sf.write(filename, myrecording, fs)
265
+ # return filename
266
  # p = pyaudio.PyAudio()
267
 
268
  # # Open the microphone stream
 
326
 
327
  option_ = ['Random Questions','Questions based on custom CSV data']
328
  Usage = st.selectbox('Select an option:', option_)
329
+
330
  if Usage == 'Questions based on custom CSV data':
331
  st.text('''
332
  You can use your own custom csv files to test this feature or
 
373
  st.success('loaded')
374
  with col4:
375
  try:
376
+ sqlOutput = gpt3(col_p) #st.text_area('SQL Query', value=gpt3(col_p))
377
  warning(sqlOutput)
378
  cars=pd.read_csv('cars.csv')
379
  result_tab2=ps.sqldf(sqlOutput)
380
  st.write(result_tab2)
381
  with open("fewshot_matplot.txt", "r") as file:
382
+ text_plot = file.read()
383
 
384
  result_tab = result_tab2.reset_index(drop=True)
385
  result_tab_string = result_tab.to_string()
 
429
 
430
  with st.form(key='columns_in_form2'):
431
  col3, col4 = st.columns(2)
432
+ with col3:
433
+ userPrompt = st.text_area("Input Prompt",'Enter Natural Language Query')
434
+ submitButton = st.form_submit_button(label = 'Submit')
435
+ if submitButton:
436
+ try:
437
+ col_p ="Create SQL statement from instruction. "+ext+" " " (" + column +")." +" Request:" + userPrompt + "SQL statement:"
438
+ result = gpt3(col_p)
439
+ except:
440
+ results = gpt3(userPrompt)
441
+ st.success('loaded')
442
+ with col4:
443
  try:
444
+ sqlOutput = gpt3(col_p) #st.text_area('SQL Query', value=gpt3(col_p))
445
+ warning(sqlOutput)
446
+ cars=pd.read_csv('cars.csv')
447
+ result_tab2=ps.sqldf(sqlOutput)
448
+ st.write(result_tab2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
449
 
450
+ with open("fewshot_matplot.txt", "r") as file:
451
+ text_plot = file.read()
452
+
453
+ result_tab = result_tab2.reset_index(drop=True)
454
+ result_tab_string = result_tab.to_string()
455
+ gr_prompt = text_plot + userPrompt + result_tab_string + "Plot graph for: "
456
+
457
+ if len(gr_prompt) > 4097:
458
+ st.write('OVERWHELMING DATA!!! You have given me more than 4097 tokens! ^_^')
459
+ st.write('As of today, the NLP model text-davinci-003 that I run on takes in inputs that have less than 4097 tokens. Kindly retry ^_^')
460
 
461
+ elif len(result_tab2.columns) < 2:
462
+ st.write("I need more data to conduct analysis and provide visualizations for you... ^_^")
463
+
464
  else:
465
+ st.success("Plotting...")
466
+ response_graph = openai.Completion.create(
467
+ engine="text-davinci-003",
468
+ prompt = gr_prompt,
469
+ max_tokens=1024,
470
+ n=1,
471
+ stop=None,
472
+ temperature=0.5,
473
+ )
474
+
475
+ if response_graph['choices'][0]['text'] != "":
476
+ print(response_graph['choices'][0]['text'])
477
+ exec(response_graph['choices'][0]['text'])
478
+
479
+ else:
480
+ print('Retry! Graph could not be plotted *_*')
481
+
482
+ except:
483
+ pass
484
 
485
 
486
  elif Usage == 'Random Questions':
 
616
  g_sheet_log(mytext, string_temp)
617
 
618
  elif Input_type == 'SPEECH':
619
+ option_speech = st.selectbox(
620
+ 'Choose from below: (Options for Transcription)',
621
+ ('Use Microphone', 'OpenAI Whisper (Upload audio file)')
622
+ )
623
+
624
+ if option_speech == 'Use Microphone':
625
+ stt_button = Button(label="Speak", width=100)
626
+ stt_button.js_on_event("button_click", CustomJS(code="""
627
+ var recognition = new webkitSpeechRecognition();
628
+ recognition.continuous = true;
629
+ recognition.interimResults = true;
630
+
631
+ recognition.onresult = function (e) {
632
+ var value = "";
633
+ for (var i = e.resultIndex; i < e.results.length; ++i) {
634
+ if (e.results[i].isFinal) {
635
+ value += e.results[i][0].transcript;
636
+ }
637
+ }
638
+ if ( value != "") {
639
+ document.dispatchEvent(new CustomEvent("GET_TEXT", {detail: value}));
640
  }
641
  }
642
+ recognition.start();
643
+ """))
644
+
645
+ result = streamlit_bokeh_events(
646
+ stt_button,
647
+ events="GET_TEXT",
648
+ key="listen",
649
+ refresh_on_update=False,
650
+ override_height=75,
651
+ debounce_time=0)
652
+
653
+ if result:
654
+ if "GET_TEXT" in result:
655
+ question = result.get("GET_TEXT")
656
+ response = openai.Completion.create(
657
+ model="text-davinci-003",
658
+ prompt=f'''Your knowledge cutoff is 2021-09, and it is not aware of any events after that time. if the
659
+ Answer to following questions is not from your knowledge base or in case of queries like weather
660
+ 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,
661
+ 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")
662
+ 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")
663
+ \nQuestion-{question}
664
+ \nAnswer -''',
665
+ temperature=0.49,
666
+ max_tokens=256,
667
+ top_p=1,
668
+ frequency_penalty=0,
669
+ presence_penalty=0
670
+ )
671
+ string_temp=response.choices[0].text
672
+
673
+ if ("gen_draw" in string_temp):
674
+ st.write('*image is being generated please wait..* ')
675
+ def extract_image_description(input_string):
676
+ return input_string.split('gen_draw("')[1].split('")')[0]
677
+ prompt=extract_image_description(string_temp)
678
+ # model_id = "CompVis/stable-diffusion-v1-4"
679
+ model_id='runwayml/stable-diffusion-v1-5'
680
+ device = "cuda"
681
 
682
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
683
+ pipe = pipe.to(device)
 
 
 
 
 
684
 
685
+ # prompt = "a photo of an astronaut riding a horse on mars"
686
+ image = pipe(prompt).images[0]
687
+
688
+ image.save("astronaut_rides_horse.png")
689
+ st.image(image)
690
+ # image
691
+
692
+ elif ("vid_tube" in string_temp):
693
+ s = Search(question)
694
+ search_res = s.results
695
+ first_vid = search_res[0]
696
+ print(first_vid)
697
+ string = str(first_vid)
698
+ video_id = string[string.index('=') + 1:-1]
699
+ # print(video_id)
700
+ YoutubeURL = "https://www.youtube.com/watch?v="
701
+ OurURL = YoutubeURL + video_id
702
+ st.write(OurURL)
703
+ st_player(OurURL)
704
+
705
+ elif ("don't" in string_temp or "internet" in string_temp ):
706
+ st.write('*searching internet*')
707
+ search_internet(question)
708
+ else:
709
+ st.write(string_temp)
710
+
711
+ elif option_speech == 'OpenAI Whisper (Upload audio file)':
712
+ audio_file = st.file_uploader("Upload Audio file",type=['wav', 'mp3'])
713
+ if audio_file is not None:
714
+ file = open(audio_file, "rb")
715
+ st.audio(audio_file)
716
+ transcription = openai.Audio.transcribe("whisper-1", file)
717
+ st.write(transcription["text"])
718
+ result = transcription["text"]
719
+ question = result
720
  response = openai.Completion.create(
721
+ model="text-davinci-003",
722
+ prompt=f'''Your knowledge cutoff is 2021-09, and it is not aware of any events after that time. if the
723
+ Answer to following questions is not from your knowledge base or in case of queries like weather
724
+ 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,
725
+ 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")
726
+ 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")
727
+ \nQuestion-{question}
728
+ \nAnswer -''',
729
+ temperature=0.49,
730
+ max_tokens=256,
731
+ top_p=1,
732
+ frequency_penalty=0,
733
+ presence_penalty=0
734
+ )
735
+ string_temp=response.choices[0].text
736
+
737
+ if ("gen_draw" in string_temp):
738
+ st.write('*image is being generated please wait..* ')
739
+ def extract_image_description(input_string):
740
+ return input_string.split('gen_draw("')[1].split('")')[0]
741
+ prompt=extract_image_description(string_temp)
742
+ # model_id = "CompVis/stable-diffusion-v1-4"
743
+ model_id='runwayml/stable-diffusion-v1-5'
744
+ device = "cuda"
745
+
746
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
747
+ pipe = pipe.to(device)
748
+
749
+ # prompt = "a photo of an astronaut riding a horse on mars"
750
+ image = pipe(prompt).images[0]
751
+
752
+ image.save("astronaut_rides_horse.png")
753
+ st.image(image)
754
+ # image
755
+
756
+ elif ("vid_tube" in string_temp):
757
+ s = Search(question)
758
+ search_res = s.results
759
+ first_vid = search_res[0]
760
+ print(first_vid)
761
+ string = str(first_vid)
762
+ video_id = string[string.index('=') + 1:-1]
763
+ # print(video_id)
764
+ YoutubeURL = "https://www.youtube.com/watch?v="
765
+ OurURL = YoutubeURL + video_id
766
+ st.write(OurURL)
767
+ st_player(OurURL)
768
+
769
+ elif ("don't" in string_temp or "internet" in string_temp ):
770
+ st.write('*searching internet*')
771
+ search_internet(question)
772
+ else:
773
+ st.write(string_temp)
 
 
774
 
775
  # except:
776
  # pass