Spaces:
Sleeping
Sleeping
| import os | |
| import openai | |
| import gradio as gr | |
| from serpapi import GoogleSearch | |
| openai.api_key = os.environ.get('MY_KEY') | |
| messages = [ | |
| { | |
| "role": "system", | |
| "content": ''' | |
| You are Dr. Vivek a professional Safety Standards Expert, Standards professional with a deep understanding | |
| of the various standards and their applications in different industries, as well as the processes for developing | |
| and revising standards. Standards experts may work in industries such as engineering, manufacturing, | |
| or quality assurance, and may be involved in developing, implementing, or auditing compliance with standards. You have | |
| great attention to detail, Analytical skills, Critical thinking, Knowledge of regulatory requirements, good Problem-solving capability. | |
| '''}, | |
| ] | |
| def g_search(prompt_): | |
| try: | |
| params = { | |
| "q": prompt_, | |
| "location": "Bengaluru, Karnataka, India", | |
| "hl": "hi", | |
| "gl": "in", | |
| "google_domain": "google.co.in", | |
| "api_key": os.environ.get('G_KEY') | |
| } | |
| search = GoogleSearch(params) | |
| results = search.get_dict() | |
| organic_results = results["organic_results"] | |
| snippets = "" | |
| counter = 1 | |
| for item in organic_results: | |
| snippets += str(counter) + ". " + item.get("snippet", "") + '\n' + item['about_this_result']['source']['source_info_link'] + '\n' | |
| counter += 1 | |
| response = openai.Completion.create( | |
| model="text-davinci-003", | |
| 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-{prompt_}\n\nAnswer-''', | |
| temperature=0.49, | |
| max_tokens=256, | |
| top_p=1, | |
| frequency_penalty=0, | |
| presence_penalty=0) | |
| string_temp = response.choices[0].text | |
| return string_temp | |
| except: | |
| pass | |
| def openai_chat(prompt_): | |
| if prompt_: | |
| messages.append( | |
| {"role": "user", "content": prompt_}, | |
| ) | |
| chat_completion = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", messages=messages | |
| ) | |
| reply = chat_completion.choices[0].message.content | |
| if ("Oh!" in reply or "Internet" in reply): | |
| res = g_search(prompt_) | |
| return res.strip() | |
| else: | |
| return reply.strip() | |
| def saab(input, hist=[]): | |
| _user_ = "π€: " | |
| _saab_ = "ππ€π: " | |
| output = openai_chat(input) | |
| hist.append((_user_+input, _saab_+output)) | |
| return hist, hist | |
| description = ''' | |
| # ππ€ Virtual Standards Support Assistant π€π | |
| Welcome to a dedicated space for Standards experts who need a hand! Our mission is to provide a robust support | |
| system for all the standards professionals out there!!''' | |
| title = "ππ€ Virtual Standards Support Assistant π€π" | |
| with gr.Blocks() as demo: | |
| gr.Markdown(description) | |
| chatbot = gr.Chatbot() | |
| state = gr.State([]) | |
| with gr.Row(): | |
| txt = gr.Textbox( | |
| show_label=False, placeholder="Ask a question and press ENTER" | |
| ).style(container=False) | |
| btn = gr.Button(value="Submit") | |
| btn.click(saab, [txt, state], [chatbot, state]) | |
| # txt.submit(saab, [txt, state], [chatbot, state]) | |
| demo.launch() | |