EnigmaOfTheWorld commited on
Commit
a1e235b
·
1 Parent(s): 2dc12cc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +366 -0
app.py ADDED
@@ -0,0 +1,366 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import warnings
3
+ import io
4
+ import pathlib
5
+ from typing import Union
6
+ import os
7
+
8
+ from PIL import Image
9
+ # import whisper
10
+ import openai
11
+ import gradio as gr
12
+ from transformers import pipeline
13
+ from stability_sdk import client
14
+ import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
15
+ from pytube import YouTube
16
+ from pytube import Search
17
+ from serpapi import GoogleSearch
18
+ import grpc
19
+ from langchain.embeddings.openai import OpenAIEmbeddings
20
+ from PyPDF2 import PdfReader
21
+ from langchain.embeddings.openai import OpenAIEmbeddings
22
+ from langchain.text_splitter import CharacterTextSplitter
23
+ from langchain.vectorstores import FAISS
24
+ from langchain.chains.question_answering import load_qa_chain
25
+ from langchain.llms import OpenAI
26
+ from langchain.agents import create_pandas_dataframe_agent
27
+ import pandas as pd
28
+ import docx
29
+
30
+
31
+
32
+ openai.api_key = os.environ['OPENAI_API_KEY']
33
+ stability_api = client.StabilityInference(
34
+ key=os.environ['STABILITY_KEY'], #os.environ("STABILITY_KEY"), # key=os.environ['STABILITY_KEY'], # API Key reference.
35
+ verbose=True, # Print debug messages.
36
+ engine="stable-diffusion-v1-5", # Set the engine to use for generation.
37
+ # Available engines: stable-diffusion-v1 stable-diffusion-v1-5 stable-diffusion-512-v2-0 stable-diffusion-768-v2-0
38
+ # stable-diffusion-512-v2-1 stable-diffusion-768-v2-1 stable-inpainting-v1-0 stable-inpainting-512-v2-0
39
+ )
40
+
41
+
42
+ whisper_from_pipeline = pipeline("automatic-speech-recognition",model="openai/whisper-medium")
43
+ EMBEDIDNGS = None
44
+ DATAFRAME_FILE = None
45
+
46
+
47
+ ############# FUNCTION DEPENDING ON IPYTHON FUNCTIONS FROM OPENAI RESPONSE
48
+ def gen_draw(user_query:str)->tuple:
49
+ ###USES STABLE DIFFUSION
50
+ answers = stability_api.generate(
51
+ prompt = user_query,
52
+ seed=992446758, # If a seed is provided, the resulting generated image will be deterministic.
53
+ # 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.
54
+ # Note: This isn't quite the case for Clip Guided generations, which we'll tackle in a future example notebook.
55
+ steps=30, # Amount of inference steps performed on image generation. Defaults to 30.
56
+ cfg_scale=8.0, # Influences how strongly your generation is guided to match your prompt.
57
+ # Setting this value higher increases the strength in which it tries to match your prompt.
58
+ # Defaults to 7.0 if not specified.
59
+ width=512, # Generation width, defaults to 512 if not included.
60
+ height=512, # Generation height, defaults to 512 if not included.
61
+ samples=1, # Number of images to generate, defaults to 1 if not included.
62
+ sampler=generation.SAMPLER_K_DPMPP_2M # Choose which sampler we want to denoise our generation with.
63
+ # Defaults to k_dpmpp_2m if not specified. Clip Guidance only supports ancestral samplers.
64
+ # (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)
65
+ )
66
+ try:
67
+ for resp in answers:
68
+ for artifact in resp.artifacts:
69
+ if artifact.finish_reason == generation.FILTER:
70
+ warnings.warn(
71
+ "Your request activated the API's safety filters and could not be processed."
72
+ "Please modify the prompt and try again.")
73
+ if artifact.type == generation.ARTIFACT_IMAGE:
74
+ img = Image.open(io.BytesIO(artifact.binary))
75
+ image_file = f'/tmp/{artifact.seed}.png'
76
+ img.save(image_file)
77
+ return (image_file,)
78
+ except grpc._channel._MultiThreadedRendezvous as e:
79
+ print(f'Exception : {e.__class__}')
80
+ print(e)
81
+ return "Invalid prompt"
82
+
83
+
84
+
85
+ def vid_tube(user_query:str) -> tuple:
86
+ py_tube_list_of_videos = Search(user_query)
87
+ first_video = py_tube_list_of_videos.results[0]
88
+ youtube_object = first_video.streams.get_highest_resolution() #if facing problems use get_lowest_resolution
89
+ file_path = youtube_object.download('/tmp/')
90
+ return (file_path,)
91
+
92
+
93
+ def search_internet(user_query:str,*,key_number:int) -> str:
94
+ if key_number >= 9:
95
+ raise gr.Error("Out of Google API Keys")
96
+ try:
97
+ params = {
98
+ "q": user_query,
99
+ "location": "Bengaluru, Karnataka, India",
100
+ "hl": "hi",
101
+ "gl": "in",
102
+ "google_domain": "google.co.in",
103
+ # "api_key": ""
104
+ "api_key": os.environ[f'GOOGLE_API{key_number}'] #os.environ("GOOGLE_API") #os.environ['GOOGLE_API']
105
+ }
106
+ search = GoogleSearch(params)
107
+ results = search.get_dict()
108
+ print(results)
109
+ organic_results = results["organic_results"]
110
+ print(f"Key {key_number} used")
111
+
112
+
113
+ snippets = ""
114
+ counter = 1
115
+ for item in organic_results:
116
+ snippets += str(counter) + ". " + item.get("snippet", "") + '\n' + item['about_this_result']['source']['source_info_link'] + '\n'
117
+ counter += 1
118
+
119
+ # snippets
120
+
121
+ response = openai.Completion.create(
122
+ model="text-davinci-003",
123
+ prompt=f'''following are snippets from google search with these as knowledge base only answer questions and print reference link as well followed by answer. \n\n {snippets}\n\n question-{user_query}\n\nAnswer-''',
124
+ temperature=0.49,
125
+ max_tokens=256,
126
+ top_p=1,
127
+ frequency_penalty=0,
128
+ presence_penalty=0)
129
+
130
+
131
+ result = response.choices[0].text
132
+
133
+ except Exception as e:
134
+ print(f'search google: ')
135
+ print(f'GOOGLE_API{key_number} OUT OF LIMIT!')
136
+ print(f'Exception: {e.__class__}, {e}')
137
+ return search_internet(user_query,key_number = key_number+1)
138
+ return result
139
+
140
+ def search_document_uploaded(user_query:str) -> str:
141
+ print('Searching uploaded document......')
142
+ docsearch = FAISS.load_local(folder_path = '/tmp/embeddings',embeddings=EMBEDIDNGS)
143
+ chain = load_qa_chain(OpenAI(), chain_type="stuff")
144
+ docs = docsearch.similarity_search(user_query)
145
+ return chain.run(input_documents=docs, question=user_query)
146
+
147
+
148
+ def ask_dataframes(user_query):
149
+ return DATAFRAME_FILE.run(user_query)
150
+
151
+ ############# GET OPENAI RESPONSE
152
+ def get_open_ai_reponse(user_query:str)->Union[tuple,str]:
153
+ print(EMBEDIDNGS)
154
+ if EMBEDIDNGS is not None:
155
+ print('Searching document')
156
+ return search_document_uploaded(user_query)
157
+
158
+ if DATAFRAME_FILE is not None:
159
+ print('Dataframe')
160
+ return ask_dataframes(user_query)
161
+
162
+
163
+ open_ai_response = openai.Completion.create(
164
+ model="text-davinci-003",
165
+ prompt=f'''Your name is GenZBot  and knowledge cutoff date is 2021-09, and you are not aware of any events after that time. if the
166
+ Answer to following questions is not from your knowledge base or in case of queries like date, time, weather
167
+ 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,
168
+ if question is related to image or painting or drawing or diagram generation then print ipython type output function gen_draw("detailed prompt of image to be generated")
169
+ 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")
170
+ 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)") .
171
+ if question is realted to sending mail or sms then print ipython type output function messenger_app(" message of us ,messenger(email,sms)")
172
+ \nQuestion-{user_query}
173
+ \nAnswer -''',
174
+ temperature=0.49,
175
+ max_tokens=256,
176
+ top_p=1,
177
+ frequency_penalty=0,
178
+ presence_penalty=0
179
+ )
180
+ result_from_open_ai = open_ai_response.choices[0].text
181
+ if 'gen_draw' in result_from_open_ai:
182
+ result = gen_draw(user_query) ## will write drawn image to file
183
+
184
+ elif 'vid_tube' in result_from_open_ai:
185
+ try:
186
+ result = vid_tube(user_query) ## play youtube video
187
+ except KeyError as e:
188
+ print(e)
189
+ result = "The system is spacing an issue please try again later"
190
+
191
+ elif ("don't" in result_from_open_ai) or ("internet" in result_from_open_ai):
192
+ result = search_internet(user_query,key_number = 1)
193
+ else:
194
+ result = result_from_open_ai
195
+ return result
196
+
197
+
198
+ ############### DIFFERENT OUTPUT FUNCTIONS
199
+ def user_input(chat_history:list,user_query:str)->list:
200
+ result = get_open_ai_reponse(user_query)
201
+ print(f'user_input: {chat_history + [(user_query,result)]}')
202
+ return chat_history + [(user_query,result)]
203
+
204
+ def transcribe(chat_history:list,user_audio_query:str)->list:
205
+ print(user_audio_query.__class__)
206
+ # text_from_speech = p(user_audio_query)["text"]
207
+ try:
208
+ user_query_from_audio = whisper_from_pipeline(user_audio_query)["text"]
209
+ except Exception as e:
210
+ print('EXCEPTION AS E')
211
+ result = f'We are having a problem : {e}'
212
+ else:
213
+ result = get_open_ai_reponse(user_query_from_audio)
214
+
215
+ # user_query_from_audio if user_query_from_audio else result
216
+ print(result)
217
+ print(f'transcribe: {chat_history + [(user_query_from_audio,result)]}')
218
+ return chat_history + [(user_query_from_audio,result)]
219
+
220
+
221
+ def pdf(file_name):
222
+ print(f'Processing {file_name} pdf file')
223
+ reader = PdfReader(file_name)
224
+ raw_text = ''
225
+ for i, page in enumerate(reader.pages):
226
+ text = page.extract_text()
227
+ if text:
228
+ raw_text += text
229
+ text_splitter = CharacterTextSplitter(
230
+ separator = "\n",
231
+ chunk_size = 1000,
232
+ chunk_overlap = 200,
233
+ length_function = len,
234
+ )
235
+ texts = text_splitter.split_text(raw_text)
236
+ return texts
237
+
238
+ def docx_file(file_name):
239
+ print(f'Processing .docx file: {file_name}')
240
+ doc = docx.Document(file_name)
241
+
242
+ # iterate over paragraphs and print their text
243
+ raw_text = ''
244
+ for para in doc.paragraphs:
245
+ raw_text += para.text
246
+ text_splitter = CharacterTextSplitter(
247
+ separator = "\n",
248
+ chunk_size = 1000,
249
+ chunk_overlap = 200,
250
+ length_function = len,
251
+ )
252
+ texts = text_splitter.split_text(raw_text)
253
+ return texts
254
+
255
+ def text_file(file_name):
256
+ print('Processing text file')
257
+ with open(file_name) as file:
258
+ raw_text = ''
259
+ for line in file:
260
+ raw_text += line
261
+ text_splitter = CharacterTextSplitter(
262
+ separator = "\n",
263
+ chunk_size = 1000,
264
+ chunk_overlap = 200,
265
+ length_function = len,
266
+ )
267
+ texts = text_splitter.split_text(raw_text)
268
+ return texts
269
+
270
+
271
+
272
+
273
+
274
+ def build_embeddings(file_name,file_ext):
275
+
276
+
277
+ functions_by_file_type = { 'pdf': pdf,
278
+ 'docx': docx_file,
279
+ 'txt': text_file
280
+
281
+ }
282
+
283
+ texts = functions_by_file_type.get(file_ext.replace('.','').strip())(file_name)
284
+ print(texts)
285
+
286
+ global EMBEDIDNGS
287
+ EMBEDIDNGS = OpenAIEmbeddings(openai_api_key=os.environ['OPENAI_API_KEY'])
288
+ docsearch = FAISS.from_texts(texts, EMBEDIDNGS)
289
+ if not os.path.exists('embeddings'):
290
+ os.mkdir('/tmp/embeddings')
291
+ docsearch.save_local('/tmp/embeddings')
292
+ print('Embeddings created.')
293
+
294
+
295
+ def ask_questions_abt_dataframes(file,file_ext):
296
+ print(file_ext)
297
+ global EMBEDIDNGS
298
+ EMBEDIDNGS = None
299
+
300
+ reader_function = { '.csv': pd.read_csv, '.xlsx': pd.read_excel }.get(file_ext)
301
+ print(reader_function.__name__)
302
+ global DATAFRAME_FILE
303
+ DATAFRAME_FILE = create_pandas_dataframe_agent(
304
+ OpenAI(openai_api_key=os.environ['OPENAI_API_KEY']),
305
+ reader_function(file.name)
306
+ )
307
+
308
+
309
+
310
+
311
+ def upload_file(chatbot_history,file_uploaded):
312
+
313
+ print(file_uploaded.__class__)
314
+ file_ext = os.path.splitext(file_uploaded.name)[-1]
315
+ if file_ext not in ['.csv','.xlsx']:
316
+ build_embeddings(file_uploaded.name,file_ext)
317
+ else:
318
+ ask_questions_abt_dataframes(file_uploaded,file_ext)
319
+
320
+
321
+ return chatbot_history + [(None, f'{os.path.split(file_uploaded.name)[-1]} uploaded start asking question')]
322
+
323
+
324
+ def clear_chat_history(history:list)->list:
325
+ history.clear()
326
+ storing_folder = pathlib.Path('/tmp/')
327
+ for file in storing_folder.iterdir():
328
+ if file.is_file():
329
+ file.unlink()
330
+
331
+ global EMBEDIDNGS
332
+ EMBEDIDNGS = None
333
+
334
+ global DATAFRAME_FILE
335
+ DATAFRAME_FILE = None
336
+ return history
337
+
338
+
339
+
340
+ #################### DRIVER SCRIPT #####################
341
+ with gr.Blocks(theme="freddyaboulton/dracula_revamped") as demo:
342
+ gr.Markdown("""<h1 style="color:white;text-align:center">GenZBot</h1>""")
343
+ gr.Markdown("""GenZBot is a virtual assistant that employs advanced artificial intelligence (AI) technologies to enhance its capabilities. Utilizing cutting-edge AI techniques such as Whisper, chatgpt, internet, Dall-E and OpenAI, GenZBot can provide users with a wide range of useful features. By leveraging AI, GenZBot can understand and respond to users' requests in a natural and intuitive manner, allowing for a more seamless and personalized experience. Its ability to generate paintings, drawings, and abstract art, as well as play music and videos, is made possible by sophisticated AI algorithms that can produce complex and nuanced results. Overall, HyperBot's extensive use of AI technology enables it to serve as a powerful and versatile digital assistant that can adapt to the needs of its users.""")
344
+ chatbot = gr.Chatbot()
345
+
346
+ with gr.Row():
347
+ with gr.Column():
348
+ user_text_query = gr.Text(label="Your Query",placeholder="Your Query")
349
+ with gr.Column(scale=0.15, min_width=0):#
350
+ user_audio_microphone_query = gr.Audio(label="Record",source="microphone",type="filepath")
351
+ user_audio_microphone_submit_button = gr.Button("Get me result")
352
+ with gr.Column(scale=0.15, min_width=0):
353
+ upload_button = gr.UploadButton("📁", file_types=["image", "video", "audio"],info="Upload text files and start talking to them")
354
+ clear_button = gr.Button("Clear chat history")
355
+
356
+
357
+ user_text_query.submit(fn=user_input,inputs=[chatbot,user_text_query],outputs=[chatbot])
358
+ user_audio_microphone_submit_button.click(fn=transcribe,inputs=[chatbot,user_audio_microphone_query],outputs=[chatbot])
359
+ clear_button.click(fn=clear_chat_history,inputs=[chatbot],outputs=[chatbot])
360
+ upload_button.upload(upload_file,inputs=[chatbot,upload_button],outputs=[chatbot])
361
+
362
+
363
+
364
+
365
+
366
+ demo.launch(debug=True)