Spaces:
Sleeping
Sleeping
File size: 3,566 Bytes
4f455b7 16d2422 7cdd214 16d2422 7cdd214 16d2422 4f455b7 16d2422 4f455b7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
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()
|