Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM as m, AutoTokenizer as t
|
2 |
+
mod=m.from_pretrained("peterpeter8585/sungyoonaimodel", token=token)
|
3 |
+
tok=t.from_pretrained("peterpeter8585/sungyoonaimodel", token=token, trust_remote_code=True)
|
4 |
+
mod.eval()
|
5 |
+
import requests
|
6 |
+
from bs4 import BeautifulSoup
|
7 |
+
import urllib
|
8 |
+
import random
|
9 |
+
import gradio as gr
|
10 |
+
chatbot = gr.Chatbot(
|
11 |
+
label="OpenGPT-4o-Chatty",
|
12 |
+
avatar_images=["user.png", "OpenAI_logo.png"],
|
13 |
+
show_copy_button=True,
|
14 |
+
likeable=True,
|
15 |
+
layout="panel"
|
16 |
+
)
|
17 |
+
|
18 |
+
# List of user agents to choose from for requests
|
19 |
+
_useragent_list = [
|
20 |
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0',
|
21 |
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
|
22 |
+
'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',
|
23 |
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
|
24 |
+
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
|
25 |
+
'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',
|
26 |
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0'
|
27 |
+
]
|
28 |
+
|
29 |
+
def get_useragent():
|
30 |
+
"""Returns a random user agent from the list."""
|
31 |
+
return random.choice(_useragent_list)
|
32 |
+
|
33 |
+
def extract_text_from_webpage(html_content):
|
34 |
+
"""Extracts visible text from HTML content using BeautifulSoup."""
|
35 |
+
soup = BeautifulSoup(html_content, "html.parser")
|
36 |
+
# Remove unwanted tags
|
37 |
+
for tag in soup(["script", "style", "header", "footer", "nav"]):
|
38 |
+
tag.extract()
|
39 |
+
# Get the remaining visible text
|
40 |
+
visible_text = soup.get_text(strip=True)
|
41 |
+
return visible_text
|
42 |
+
|
43 |
+
def search(term, num_results=1, lang="ko", advanced=True, sleep_interval=0, timeout=5, safe="active", ssl_verify=None):
|
44 |
+
"""Performs a Google search and returns the results."""
|
45 |
+
escaped_term = urllib.parse.quote_plus(term)
|
46 |
+
start = 0
|
47 |
+
all_results = []
|
48 |
+
|
49 |
+
# Fetch results in batches
|
50 |
+
while start < num_results:
|
51 |
+
resp = requests.get(
|
52 |
+
url="https://www.google.com/search",
|
53 |
+
headers={"User-Agent": get_useragent()}, # Set random user agent
|
54 |
+
params={
|
55 |
+
"q": term,
|
56 |
+
"num": num_results - start, # Number of results to fetch in this batch
|
57 |
+
"hl": lang,
|
58 |
+
"start": start,
|
59 |
+
"safe": safe,
|
60 |
+
},
|
61 |
+
timeout=timeout,
|
62 |
+
verify=ssl_verify,
|
63 |
+
)
|
64 |
+
resp.raise_for_status() # Raise an exception if request fails
|
65 |
+
|
66 |
+
soup = BeautifulSoup(resp.text, "html.parser")
|
67 |
+
result_block = soup.find_all("div", attrs={"class": "g"})
|
68 |
+
|
69 |
+
# If no results, continue to the next batch
|
70 |
+
if not result_block:
|
71 |
+
start += 1
|
72 |
+
continue
|
73 |
+
|
74 |
+
# Extract link and text from each result
|
75 |
+
for result in result_block:
|
76 |
+
link = result.find("a", href=True)
|
77 |
+
if link:
|
78 |
+
link = link["href"]
|
79 |
+
try:
|
80 |
+
# Fetch webpage content
|
81 |
+
webpage = requests.get(link, headers={"User-Agent": get_useragent()})
|
82 |
+
webpage.raise_for_status()
|
83 |
+
# Extract visible text from webpage
|
84 |
+
visible_text = extract_text_from_webpage(webpage.text)
|
85 |
+
all_results.append({"link": link, "text": visible_text})
|
86 |
+
except requests.exceptions.RequestException as e:
|
87 |
+
# Handle errors fetching or processing webpage
|
88 |
+
print(f"Error fetching or processing {link}: {e}")
|
89 |
+
all_results.append({"link": link, "text": None})
|
90 |
+
else:
|
91 |
+
all_results.append({"link": None, "text": None})
|
92 |
+
|
93 |
+
start += len(result_block) # Update starting index for next batch
|
94 |
+
|
95 |
+
return all_results
|
96 |
+
|
97 |
+
def chat(message,
|
98 |
+
history: list[tuple[str, str]],
|
99 |
+
system_message,
|
100 |
+
max_tokens,
|
101 |
+
temperature,
|
102 |
+
top_p, search_key=""):
|
103 |
+
if search_key=="":
|
104 |
+
pass
|
105 |
+
else:
|
106 |
+
s=search(term=search_key)
|
107 |
+
messages=[{"role":"system","content":system_message+f"And, your name is chatchat made by an elementry school student who's huggingface id is peterpeter8585.The user who is useing you, gave you an information about the question he(she) is going to ask.INFO:{s}"}]
|
108 |
+
for val in history:
|
109 |
+
if val[0]:
|
110 |
+
messages.append({"role": "user", "content": val[0]})
|
111 |
+
if val[1]:
|
112 |
+
messages.append({"role": "assistant", "content": val[1]})
|
113 |
+
|
114 |
+
messages.append({"role": "user", "content": message})
|
115 |
+
input_ids=tok.apply_chat_template(messages, add_generation_prompt=True,return_tensors="pt")
|
116 |
+
with torch.no_grad():
|
117 |
+
o=mod.generate(input_ids, max_new_tokens=256,do_sample=True,temperature=0.7,top_p=0.9)[0][input_ids.shape[-1]:]
|
118 |
+
ans=tok.decode(o, skip_special_tokens=True)
|
119 |
+
yield ans
|
120 |
+
ai1=gr.ChatInterface(
|
121 |
+
chat,
|
122 |
+
chatbot=chatbot,
|
123 |
+
additional_inputs=[
|
124 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message", interactive=True),
|
125 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
126 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.1, step=0.1, label="Temperature"),
|
127 |
+
gr.Slider(
|
128 |
+
minimum=0.1,
|
129 |
+
maximum=1.0,
|
130 |
+
value=0.1,
|
131 |
+
step=0.05,
|
132 |
+
label="Top-p (nucleus sampling)",
|
133 |
+
),
|
134 |
+
gr.Textbox(label="Search Keyword")
|
135 |
+
],
|
136 |
+
)
|
137 |
+
with gr.Blocks(theme="prithivMLmods/Minecraft-Theme") as ai:
|
138 |
+
gr.TabbedInterface([ai1],["Chatchat"])
|
139 |
+
ai.launch()
|