Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,161 +1,155 @@
|
|
1 |
-
import os
|
2 |
-
import urllib
|
3 |
-
import requests
|
4 |
-
from bs4 import BeautifulSoup
|
5 |
-
import torch
|
6 |
import gradio as gr
|
7 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
8 |
import logging
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
logging.basicConfig(level=logging.DEBUG)
|
12 |
logger = logging.getLogger(__name__)
|
13 |
|
14 |
-
|
15 |
-
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
-
MODEL_NAME = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
# Function to perform a Google search and return the results
|
30 |
-
def search(term, num_results=2, lang="en", timeout=5, safe="active", ssl_verify=None):
|
31 |
-
logger.debug(f"Starting search for term: {term}")
|
32 |
-
escaped_term = urllib.parse.quote_plus(term)
|
33 |
-
start = 0
|
34 |
-
all_results = []
|
35 |
-
max_chars_per_page = 8000
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
resp = session.get(
|
41 |
-
url="https://www.google.com/search",
|
42 |
-
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"},
|
43 |
-
params={
|
44 |
-
"q": term,
|
45 |
-
"num": num_results - start,
|
46 |
-
"hl": lang,
|
47 |
-
"start": start,
|
48 |
-
"safe": safe,
|
49 |
-
},
|
50 |
-
timeout=timeout,
|
51 |
-
verify=ssl_verify,
|
52 |
-
)
|
53 |
-
resp.raise_for_status()
|
54 |
-
soup = BeautifulSoup(resp.text, "html.parser")
|
55 |
-
result_block = soup.find_all("div", attrs={"class": "g"})
|
56 |
-
if not result_block:
|
57 |
-
start += 1
|
58 |
-
continue
|
59 |
-
for result in result_block:
|
60 |
-
link = result.find("a", href=True)
|
61 |
-
if link:
|
62 |
-
link = link["href"]
|
63 |
-
try:
|
64 |
-
webpage = session.get(link, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"})
|
65 |
-
webpage.raise_for_status()
|
66 |
-
visible_text = extract_text_from_webpage(webpage.text)
|
67 |
-
if len(visible_text) > max_chars_per_page:
|
68 |
-
visible_text = visible_text[:max_chars_per_page] + "..."
|
69 |
-
all_results.append({"link": link, "text": visible_text})
|
70 |
-
except requests.exceptions.RequestException as e:
|
71 |
-
logger.error(f"Error fetching or processing {link}: {e}")
|
72 |
-
all_results.append({"link": link, "text": None})
|
73 |
-
else:
|
74 |
-
all_results.append({"link": None, "text": None})
|
75 |
-
start += len(result_block)
|
76 |
-
except Exception as e:
|
77 |
-
logger.error(f"Error during search: {e}")
|
78 |
-
break
|
79 |
-
logger.debug(f"Search results: {all_results}")
|
80 |
-
return all_results
|
81 |
|
82 |
-
# Function to extract visible text from HTML content
|
83 |
def extract_text_from_webpage(html_content):
|
|
|
84 |
soup = BeautifulSoup(html_content, "html.parser")
|
|
|
85 |
for tag in soup(["script", "style", "header", "footer", "nav"]):
|
86 |
tag.extract()
|
|
|
87 |
visible_text = soup.get_text(strip=True)
|
88 |
return visible_text
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
prompt += f"User: {item[0]}\nAssistant: {item[1]}\n"
|
96 |
-
prompt += f"User: {user_prompt}\nAssistant:"
|
97 |
-
logger.debug(f"Formatted prompt: {prompt}")
|
98 |
-
return prompt
|
99 |
|
100 |
-
#
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
115 |
|
116 |
-
|
117 |
-
|
|
|
|
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
136 |
else:
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
else:
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
# Define Gradio interface components
|
161 |
max_new_tokens = gr.Slider(
|
@@ -189,71 +183,27 @@ temperature = gr.Slider(
|
|
189 |
minimum=0.0,
|
190 |
maximum=2.0,
|
191 |
value=0.5,
|
192 |
-
step=0.
|
193 |
-
visible=True,
|
194 |
interactive=True,
|
195 |
-
label="
|
196 |
-
info="
|
197 |
-
)
|
198 |
-
top_p = gr.Slider(
|
199 |
-
minimum=0.01,
|
200 |
-
maximum=0.99,
|
201 |
-
value=0.9,
|
202 |
-
step=0.01,
|
203 |
-
visible=True,
|
204 |
-
interactive=True,
|
205 |
-
label="Top P",
|
206 |
-
info="Higher values are equivalent to sampling more low-probability tokens.",
|
207 |
-
)
|
208 |
-
|
209 |
-
# Create a chatbot interface
|
210 |
-
chatbot = gr.Chatbot(
|
211 |
-
label="OpenGPT-4o-Chatty",
|
212 |
-
show_copy_button=True,
|
213 |
-
likeable=True,
|
214 |
-
layout="panel"
|
215 |
)
|
|
|
216 |
|
217 |
-
# Define Gradio interface
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
user_input,
|
224 |
-
history,
|
225 |
web_search,
|
226 |
temperature,
|
227 |
max_new_tokens,
|
228 |
repetition_penalty,
|
229 |
-
top_p,
|
230 |
-
tokenizer=tokenizer # Pass tokenizer to model_inference
|
231 |
-
)
|
232 |
-
history.append((user_input["text"], response))
|
233 |
-
logger.debug(f"Updated chat history: {history}")
|
234 |
-
return history, history
|
235 |
-
|
236 |
-
# Create Gradio interface
|
237 |
-
interface = gr.Interface(
|
238 |
-
fn=chat_interface,
|
239 |
-
inputs=[
|
240 |
-
gr.Textbox(label="User Input"),
|
241 |
-
gr.State([]),
|
242 |
-
gr.Checkbox(label="Web Search", value=True),
|
243 |
decoding_strategy,
|
244 |
-
temperature,
|
245 |
-
max_new_tokens,
|
246 |
-
repetition_penalty,
|
247 |
-
top_p
|
248 |
],
|
249 |
-
outputs=
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
description="An AI assistant capable of insightful conversations and web search."
|
255 |
-
)
|
256 |
-
|
257 |
-
if __name__ == "__main__":
|
258 |
-
logger.debug("Launching Gradio interface")
|
259 |
-
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
import logging
|
5 |
+
import random
|
6 |
+
import requests
|
7 |
+
import urllib
|
8 |
+
from bs4 import BeautifulSoup
|
9 |
+
import os
|
10 |
|
11 |
+
# Initialize logging
|
12 |
logging.basicConfig(level=logging.DEBUG)
|
13 |
logger = logging.getLogger(__name__)
|
14 |
|
15 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
16 |
|
17 |
+
# List of user agents to choose from for requests
|
18 |
+
_useragent_list = [
|
19 |
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0',
|
20 |
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
|
21 |
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
|
22 |
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
|
23 |
+
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
|
24 |
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62',
|
25 |
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0'
|
26 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
def get_useragent():
|
29 |
+
"""Returns a random user agent from the list."""
|
30 |
+
return random.choice(_useragent_list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
|
|
32 |
def extract_text_from_webpage(html_content):
|
33 |
+
"""Extracts visible text from HTML content using BeautifulSoup."""
|
34 |
soup = BeautifulSoup(html_content, "html.parser")
|
35 |
+
# Remove unwanted tags
|
36 |
for tag in soup(["script", "style", "header", "footer", "nav"]):
|
37 |
tag.extract()
|
38 |
+
# Get the remaining visible text
|
39 |
visible_text = soup.get_text(strip=True)
|
40 |
return visible_text
|
41 |
|
42 |
+
def search(term, num_results=1, lang="en", advanced=True, sleep_interval=0, timeout=5, safe="active", ssl_verify=None):
|
43 |
+
"""Performs a Google search and returns the results."""
|
44 |
+
escaped_term = urllib.parse.quote_plus(term)
|
45 |
+
start = 0
|
46 |
+
all_results = []
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
# Fetch results in batches
|
49 |
+
while start < num_results:
|
50 |
+
resp = requests.get(
|
51 |
+
url="https://www.google.com/search",
|
52 |
+
headers={"User-Agent": get_useragent()}, # Set random user agent
|
53 |
+
params={
|
54 |
+
"q": term,
|
55 |
+
"num": num_results - start, # Number of results to fetch in this batch
|
56 |
+
"hl": lang,
|
57 |
+
"start": start,
|
58 |
+
"safe": safe,
|
59 |
+
},
|
60 |
+
timeout=timeout,
|
61 |
+
verify=ssl_verify,
|
62 |
+
)
|
63 |
+
resp.raise_for_status() # Raise an exception if request fails
|
64 |
+
|
65 |
+
soup = BeautifulSoup(resp.text, "html.parser")
|
66 |
+
result_block = soup.find_all("div", attrs={"class": "g"})
|
67 |
|
68 |
+
# If no results, continue to the next batch
|
69 |
+
if not result_block:
|
70 |
+
start += 1
|
71 |
+
continue
|
72 |
|
73 |
+
# Extract link and text from each result
|
74 |
+
for result in result_block:
|
75 |
+
link = result.find("a", href=True)
|
76 |
+
if link:
|
77 |
+
link = link["href"]
|
78 |
+
try:
|
79 |
+
# Fetch webpage content
|
80 |
+
webpage = requests.get(link, headers={"User-Agent": get_useragent()})
|
81 |
+
webpage.raise_for_status()
|
82 |
+
# Extract visible text from webpage
|
83 |
+
visible_text = extract_text_from_webpage(webpage.text)
|
84 |
+
all_results.append({"link": link, "text": visible_text})
|
85 |
+
except requests.exceptions.RequestException as e:
|
86 |
+
# Handle errors fetching or processing webpage
|
87 |
+
print(f"Error fetching or processing {link}: {e}")
|
88 |
+
all_results.append({"link": link, "text": None})
|
|
|
89 |
else:
|
90 |
+
all_results.append({"link": None, "text": None})
|
91 |
+
|
92 |
+
start += len(result_block) # Update starting index for next batch
|
93 |
+
|
94 |
+
return all_results
|
95 |
+
|
96 |
+
# Load the model and tokenizer
|
97 |
+
model_name = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
98 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
99 |
+
model = AutoModelForCausalLM.from_pretrained(model_name).to(DEVICE)
|
100 |
+
|
101 |
+
def format_prompt(user_input, chat_history):
|
102 |
+
prompt = ""
|
103 |
+
for user, bot in chat_history:
|
104 |
+
prompt += f"User: {user}\nBot: {bot}\n"
|
105 |
+
prompt += f"User: {user_input}\nBot: "
|
106 |
+
return prompt
|
107 |
+
|
108 |
+
def model_inference(user_prompt, chat_history, web_search, temperature, max_new_tokens, repetition_penalty, top_p):
|
109 |
+
try:
|
110 |
+
if not user_prompt["files"]:
|
111 |
+
if web_search:
|
112 |
+
logger.debug("Performing web search")
|
113 |
+
web_results = search(user_prompt["text"], num_results=3) # Fetching more results for better context
|
114 |
+
web2 = ' '.join([f"Link: {res['link']}\nText: {res['text']}\n\n" for res in web_results if res['text']])
|
115 |
+
formatted_prompt = format_prompt(f"{user_prompt['text']} [WEB] {web2}", chat_history)
|
116 |
+
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(DEVICE)
|
117 |
+
if model:
|
118 |
+
outputs = model.generate(
|
119 |
+
**inputs,
|
120 |
+
max_new_tokens=max_new_tokens,
|
121 |
+
repetition_penalty=repetition_penalty,
|
122 |
+
do_sample=True,
|
123 |
+
temperature=temperature,
|
124 |
+
top_p=top_p
|
125 |
+
)
|
126 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
127 |
+
else:
|
128 |
+
response = "Model is not available. Please try again later."
|
129 |
+
logger.debug(f"Model response: {response}")
|
130 |
+
return response
|
131 |
else:
|
132 |
+
formatted_prompt = format_prompt(user_prompt["text"], chat_history)
|
133 |
+
inputs = tokenizer(formatted_prompt, return_tensors="pt").to(DEVICE)
|
134 |
+
if model:
|
135 |
+
outputs = model.generate(
|
136 |
+
**inputs,
|
137 |
+
max_new_tokens=max_new_tokens,
|
138 |
+
repetition_penalty=repetition_penalty,
|
139 |
+
do_sample=True,
|
140 |
+
temperature=temperature,
|
141 |
+
top_p=top_p
|
142 |
+
)
|
143 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
144 |
+
else:
|
145 |
+
response = "Model is not available. Please try again later."
|
146 |
+
logger.debug(f"Model response: {response}")
|
147 |
+
return response
|
148 |
+
else:
|
149 |
+
return "Image input not supported in this implementation."
|
150 |
+
except Exception as e:
|
151 |
+
logger.error(f"Error during model inference: {e}")
|
152 |
+
return "An error occurred during model inference. Please try again."
|
153 |
|
154 |
# Define Gradio interface components
|
155 |
max_new_tokens = gr.Slider(
|
|
|
183 |
minimum=0.0,
|
184 |
maximum=2.0,
|
185 |
value=0.5,
|
186 |
+
step=0.1,
|
|
|
187 |
interactive=True,
|
188 |
+
label="Temperature",
|
189 |
+
info="Control randomness: lower temperature produces less randomness.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
)
|
191 |
+
web_search = gr.Checkbox(label="Enable Web Search", default=False, description="Enable web search for better responses")
|
192 |
|
193 |
+
# Define the Gradio interface
|
194 |
+
gr.Interface(
|
195 |
+
fn=model_inference,
|
196 |
+
inputs=[
|
197 |
+
gr.Textbox(label="User Input", placeholder="Type your input here..."),
|
198 |
+
gr.MultiText(label="Chat History", placeholder="User: ...\nBot: ...", optional=True),
|
|
|
|
|
199 |
web_search,
|
200 |
temperature,
|
201 |
max_new_tokens,
|
202 |
repetition_penalty,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
decoding_strategy,
|
|
|
|
|
|
|
|
|
204 |
],
|
205 |
+
outputs=gr.Textbox(label="AI Response"),
|
206 |
+
live=True,
|
207 |
+
title="OpenGPT 4o Demo",
|
208 |
+
description="An AI-powered assistant that can chat with you and provide informative responses.",
|
209 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|