File size: 3,070 Bytes
16d2422
 
 
 
7cdd214
16d2422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cdd214
16d2422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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

gr.Interface(fn=saab,
             inputs=["text","state"],
             outputs=["chatbot","state"],
             title="SSB Virtual Coaching",
             description="Ask your doubts to Col. sir! (Dev. Vishwesh V Bhat)").launch(share=True)