Shreyas094 commited on
Commit
a2335c5
1 Parent(s): 753d9d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -17
app.py CHANGED
@@ -1,30 +1,135 @@
 
 
 
1
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
2
  import torch
 
3
 
4
- # Path to the locally saved quantized model directory
5
- model_path = '/path/to/your/quantized_model_directory'
 
 
 
6
 
7
- # Load tokenizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
9
 
10
- # Load quantized model
11
- quantized_model = AutoModelForCausalLM.from_pretrained(model_path)
 
12
 
13
- # Check if a GPU is available and move model to GPU if available
14
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
- quantized_model.to(device)
16
 
17
- # Example text input
18
- text_input = "How did Tesla perform in Q1 2024?"
19
 
20
- # Tokenize input
21
- inputs = tokenizer(text_input, return_tensors="pt").to(device)
22
 
23
- # Generate response
24
- outputs = quantized_model.generate(**inputs, max_length=150, do_sample=False)
25
 
26
- # Decode generated tokens to readable string
27
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
 
29
- # Print generated response
30
- print(f"Generated response: {response}")
 
1
+ import random
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
5
+ from huggingface_hub import login
6
  import torch
7
+ import os
8
 
9
+ # Ensure sentencepiece is installed
10
+ try:
11
+ import sentencepiece
12
+ except ImportError:
13
+ raise ImportError("Please install the sentencepiece library using `pip install sentencepiece`.")
14
 
15
+ # Retrieve the Hugging Face token from secrets (replace 'HUGGINGFACE_TOKEN' with your secret key)
16
+ hf_token = os.getenv('HUGGINGFACE_TOKEN')
17
+
18
+ # Log in to Hugging Face
19
+ login(token=hf_token)
20
+
21
+ # List of user agents
22
+ _useragent_list = [
23
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
24
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
25
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Edge/91.0.864.59 Safari/537.36",
26
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Edge/91.0.864.59 Safari/537.36",
27
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36",
28
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36",
29
+ ]
30
+
31
+ # Function to extract visible text from HTML content of a webpage
32
+ def extract_text_from_webpage(html):
33
+ print("Extracting text from webpage...")
34
+ soup = BeautifulSoup(html, 'html.parser')
35
+ for script in soup(["script", "style"]):
36
+ script.extract() # Remove scripts and styles
37
+ text = soup.get_text()
38
+ lines = (line.strip() for line in text.splitlines())
39
+ chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
40
+ text = '\n'.join(chunk for chunk in chunks if chunk)
41
+ print(f"Extracted text length: {len(text)}")
42
+ return text
43
+
44
+ # Function to perform a Google search and retrieve results
45
+ def google_search(term, num_results=5, lang="en", timeout=5, safe="active", ssl_verify=None):
46
+ """Performs a Google search and returns the results."""
47
+ print(f"Searching for term: {term}")
48
+ escaped_term = requests.utils.quote(term)
49
+ start = 0
50
+ all_results = []
51
+ max_chars_per_page = 8000 # Limit the number of characters from each webpage to stay under the token limit
52
+
53
+ with requests.Session() as session:
54
+ while start < num_results:
55
+ print(f"Fetching search results starting from: {start}")
56
+ try:
57
+ # Choose a random user agent
58
+ user_agent = random.choice(_useragent_list)
59
+ headers = {
60
+ 'User-Agent': user_agent
61
+ }
62
+ print(f"Using User-Agent: {headers['User-Agent']}")
63
+
64
+ resp = session.get(
65
+ url="https://www.google.com/search",
66
+ headers=headers,
67
+ params={
68
+ "q": term,
69
+ "num": num_results - start,
70
+ "hl": lang,
71
+ "start": start,
72
+ "safe": safe,
73
+ },
74
+ timeout=timeout,
75
+ verify=ssl_verify,
76
+ )
77
+ resp.raise_for_status()
78
+ except requests.exceptions.RequestException as e:
79
+ print(f"Error fetching search results: {e}")
80
+ break
81
+
82
+ soup = BeautifulSoup(resp.text, "html.parser")
83
+ result_block = soup.find_all("div", attrs={"class": "g"})
84
+ if not result_block:
85
+ print("No more results found.")
86
+ break
87
+ for result in result_block:
88
+ link = result.find("a", href=True)
89
+ if link:
90
+ link = link["href"]
91
+ print(f"Found link: {link}")
92
+ try:
93
+ webpage = session.get(link, headers=headers, timeout=timeout)
94
+ webpage.raise_for_status()
95
+ visible_text = extract_text_from_webpage(webpage.text)
96
+ if len(visible_text) > max_chars_per_page:
97
+ visible_text = visible_text[:max_chars_per_page] + "..."
98
+ all_results.append({"link": link, "text": visible_text})
99
+ except requests.exceptions.RequestException as e:
100
+ print(f"Error fetching or processing {link}: {e}")
101
+ all_results.append({"link": link, "text": None})
102
+ else:
103
+ print("No link found in result.")
104
+ all_results.append({"link": None, "text": None})
105
+ start += len(result_block)
106
+ print(f"Total results fetched: {len(all_results)}")
107
+ return all_results
108
+
109
+ # Load the Mixtral-8x7B-Instruct model and tokenizer
110
+ model_name = 'mistralai/Mistral-7B-Instruct-v0.3'
111
  tokenizer = AutoTokenizer.from_pretrained(model_name)
112
+ model = AutoModelForCausalLM.from_pretrained(model_name)
113
 
114
+ # Check if a GPU is available and if not, fall back to CPU
115
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Check for GPU
116
+ model.to(device) # Move model to the device
117
 
118
+ # Example usage
119
+ search_term = "How did Tesla perform in Q1 2024"
120
+ search_results = google_search(search_term, num_results=3)
121
 
122
+ # Combine text from search results to create a prompt
123
+ combined_text = "\n\n".join(result['text'] for result in search_results if result['text'])
124
 
125
+ # Tokenize the input text
126
+ inputs = tokenizer(combined_text, return_tensors="pt").to(device) # Move inputs to the device
127
 
128
+ # Generate a response
129
+ outputs = model.generate(**inputs, max_length=150, temperature=0.7, top_p=0.9, top_k=50)
130
 
131
+ # Decode the generated tokens to a readable string
132
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
133
 
134
+ # Print the response
135
+ print(response)