BillBojangeles2000 commited on
Commit
9f3f2dd
1 Parent(s): 106fd84

Create code.py

Browse files
Files changed (1) hide show
  1. code.py +122 -0
code.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Assistant.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1tbapNWoymz_Oq_M96lTAeUDZrvKcB2ji
8
+ """
9
+
10
+ #Define Web Scraping function
11
+ def web_scrape(url, tag, id):
12
+ headers = {
13
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
14
+ }
15
+ page = requests.get(url, headers=headers)
16
+ soup = BeautifulSoup(page.content, 'html.parser')
17
+ data = soup.find_all(tag, class_ = id)
18
+ res = []
19
+ i = 0
20
+ while i < 6:
21
+ res.append(data[i].get_text())
22
+ i = i+1
23
+ return res
24
+
25
+ #Define Google result scraping function
26
+ def google_scrape(query):
27
+ url = "https://www.google.com/search?q=" + str(query)
28
+ res = web_scrape(url, "div", "VwiC3b yXK7lf MUxGbd yDYNvb lyLwlc lEBKkf")
29
+ return res
30
+
31
+ #Define Bratanica web scraping function
32
+ def bratanica_scrape(query):
33
+ headers = {
34
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
35
+ }
36
+ url = "https://www.britannica.com"
37
+ page = requests.get(url + "/search?query=" + query, headers=headers)
38
+ soup = BeautifulSoup(page.content, 'html.parser')
39
+ link = soup.find_all("a", class_ ="font-weight-bold font-18")
40
+ page = requests.get(url+link[0]["href"], headers=headers)
41
+ soup = BeautifulSoup(page.content, 'html.parser')
42
+ data = soup.find_all("p")
43
+ res = []
44
+ for i in data:
45
+ res.append(i.get_text())
46
+ return res
47
+
48
+ import requests
49
+ from bs4 import BeautifulSoup
50
+
51
+ import torch
52
+ from transformers import LlamaForCausalLM, LlamaTokenizer
53
+
54
+ # Hugging Face model_path
55
+ model_path = 'psmathur/orca_mini_3b'
56
+ tokenizer = LlamaTokenizer.from_pretrained(model_path)
57
+ model = LlamaForCausalLM.from_pretrained(
58
+ model_path, torch_dtype=torch.float16, device_map='auto', load_in_4bit = True
59
+ )
60
+
61
+ #generate response function
62
+ def generate_text(system, instruction, gen_len, input=None):
63
+
64
+ if input:
65
+ prompt = f"### System:\n{system}\n\n### User:\n{instruction}\n\n### Context:\n{input}\n\n### Response:\n"
66
+ else:
67
+ prompt = f"### System:\n{system}\n\n### User:\n{instruction}\n\n### Response:\n"
68
+
69
+ tokens = tokenizer.encode(prompt)
70
+ tokens = torch.LongTensor(tokens).unsqueeze(0)
71
+ tokens = tokens.to('cuda')
72
+
73
+ instance = {'input_ids': tokens,'top_p': 1.5, 'temperature':0.3, 'generate_len': gen_len, 'top_k': 200}
74
+
75
+ length = len(tokens[0])
76
+ with torch.no_grad():
77
+ rest = model.generate(
78
+ input_ids=tokens,
79
+ max_length=length+instance['generate_len'],
80
+ use_cache=True,
81
+ do_sample=True,
82
+ top_p=instance['top_p'],
83
+ temperature=instance['temperature'],
84
+ top_k=instance['top_k']
85
+ )
86
+ output = rest[0][length:]
87
+ string = tokenizer.decode(output, skip_special_tokens=True)
88
+ return f'{string}'
89
+
90
+ def context(query):
91
+ headers = {
92
+ 'Authorization': 'Bearer FGSCPX4IMD3MGEQHWOORJDR73RHT7LFZ',
93
+ }
94
+
95
+ params = {
96
+ 'v': '20230820',
97
+ 'q': query,
98
+ }
99
+
100
+ response = requests.get('https://api.wit.ai/message', params=params, headers=headers).json()
101
+
102
+ return response["intents"][0]["name"]
103
+
104
+ convo = []
105
+ while True:
106
+ q = input("Human:")
107
+ convo.append(convo)
108
+
109
+ if (context(q)) == "Does_not_require_context":
110
+ system = "You are an AI chatbot that aims to be as helpful and funny as possible. You will be given the past conversation history to know what you were talking about before"
111
+ instruction = q
112
+ res = generate_text(system, instruction, 512, convo)
113
+ convo.append("Bot:" + res)
114
+ print(res)
115
+ elif (context(q)) == "Requires_additional_context":
116
+ system = "You are an AI chatbot that aims to be as helpful as possible. To do so, using the context, answer the question that the user asks you."
117
+ instruction = q
118
+ clue = google_scrape(q)
119
+ res = generate_text(system, instruction, 512, clue[0])
120
+ convo.append("Bot:" + res)
121
+ print(res)
122
+