Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from bs4 import BeautifulSoup
|
|
5 |
import torch
|
6 |
import gradio as gr
|
7 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
|
|
8 |
import logging
|
9 |
|
10 |
# Set up logging
|
@@ -26,6 +27,7 @@ except Exception as e:
|
|
26 |
model = None
|
27 |
tokenizer = None
|
28 |
|
|
|
29 |
assert tokenizer is not None, "Tokenizer failed to load and is None"
|
30 |
|
31 |
# Function to perform a Google search and return the results
|
@@ -83,15 +85,11 @@ def search(term, num_results=2, lang="en", timeout=5, safe="active", ssl_verify=
|
|
83 |
|
84 |
# Function to extract visible text from HTML content
|
85 |
def extract_text_from_webpage(html_content):
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
return visible_text
|
92 |
-
except Exception as e:
|
93 |
-
logger.error(f"Error extracting text from webpage: {e}")
|
94 |
-
return ""
|
95 |
|
96 |
# Function to format the prompt for the language model
|
97 |
def format_prompt(user_prompt, chat_history):
|
@@ -168,7 +166,7 @@ def model_inference(
|
|
168 |
return "Image input not supported in this implementation."
|
169 |
|
170 |
# Define Gradio interface components
|
171 |
-
|
172 |
minimum=2048,
|
173 |
maximum=16000,
|
174 |
value=4096,
|
@@ -176,7 +174,7 @@ max_new_tokens_slider = gr.Slider(
|
|
176 |
interactive=True,
|
177 |
label="Maximum number of new tokens to generate",
|
178 |
)
|
179 |
-
|
180 |
minimum=0.01,
|
181 |
maximum=5.0,
|
182 |
value=1,
|
@@ -185,7 +183,7 @@ repetition_penalty_slider = gr.Slider(
|
|
185 |
label="Repetition penalty",
|
186 |
info="1.0 is equivalent to no penalty",
|
187 |
)
|
188 |
-
|
189 |
[
|
190 |
"Greedy",
|
191 |
"Top P Sampling",
|
@@ -195,7 +193,7 @@ decoding_strategy_radio = gr.Radio(
|
|
195 |
interactive=True,
|
196 |
info="Higher values are equivalent to sampling more low-probability tokens.",
|
197 |
)
|
198 |
-
|
199 |
minimum=0.0,
|
200 |
maximum=2.0,
|
201 |
value=0.5,
|
@@ -205,7 +203,7 @@ temperature_slider = gr.Slider(
|
|
205 |
label="Sampling temperature",
|
206 |
info="Higher values will produce more diverse outputs.",
|
207 |
)
|
208 |
-
|
209 |
minimum=0.01,
|
210 |
maximum=0.99,
|
211 |
value=0.9,
|
@@ -217,14 +215,14 @@ top_p_slider = gr.Slider(
|
|
217 |
)
|
218 |
|
219 |
# Create a chatbot interface
|
220 |
-
|
221 |
label="OpenGPT-4o-Chatty",
|
222 |
show_copy_button=True,
|
223 |
likeable=True,
|
224 |
layout="panel"
|
225 |
)
|
226 |
|
227 |
-
# Define Gradio interface
|
228 |
def chat_interface(user_input, history, web_search, decoding_strategy, temperature, max_new_tokens, repetition_penalty, top_p):
|
229 |
logger.debug(f"Chat interface called with user_input: {user_input}")
|
230 |
if isinstance(user_input, str):
|
@@ -240,27 +238,31 @@ def chat_interface(user_input, history, web_search, decoding_strategy, temperatu
|
|
240 |
top_p,
|
241 |
tokenizer=tokenizer # Pass tokenizer to model_inference
|
242 |
)
|
243 |
-
history.append((user_input
|
|
|
244 |
return history, history
|
245 |
|
246 |
-
# Create
|
247 |
interface = gr.Interface(
|
248 |
fn=chat_interface,
|
249 |
inputs=[
|
250 |
-
gr.Textbox(
|
251 |
-
gr.State(),
|
252 |
-
gr.Checkbox(label="
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
|
|
|
|
|
|
|
|
258 |
],
|
259 |
-
outputs=[chatbot_interface, gr.State()],
|
260 |
title="OpenGPT-4o-Chatty",
|
261 |
-
description="
|
262 |
-
allow_flagging="never",
|
263 |
)
|
264 |
|
265 |
if __name__ == "__main__":
|
266 |
-
|
|
|
|
5 |
import torch
|
6 |
import gradio as gr
|
7 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
8 |
+
from huggingface_hub import InferenceClient
|
9 |
import logging
|
10 |
|
11 |
# Set up logging
|
|
|
27 |
model = None
|
28 |
tokenizer = None
|
29 |
|
30 |
+
# Assert to ensure tokenizer is loaded
|
31 |
assert tokenizer is not None, "Tokenizer failed to load and is None"
|
32 |
|
33 |
# Function to perform a Google search and return the results
|
|
|
85 |
|
86 |
# Function to extract visible text from HTML content
|
87 |
def extract_text_from_webpage(html_content):
|
88 |
+
soup = BeautifulSoup(html_content, "html.parser")
|
89 |
+
for tag in soup(["script", "style", "header", "footer", "nav"]):
|
90 |
+
tag.extract()
|
91 |
+
visible_text = soup.get_text(strip=True)
|
92 |
+
return visible_text
|
|
|
|
|
|
|
|
|
93 |
|
94 |
# Function to format the prompt for the language model
|
95 |
def format_prompt(user_prompt, chat_history):
|
|
|
166 |
return "Image input not supported in this implementation."
|
167 |
|
168 |
# Define Gradio interface components
|
169 |
+
max_new_tokens = gr.Slider(
|
170 |
minimum=2048,
|
171 |
maximum=16000,
|
172 |
value=4096,
|
|
|
174 |
interactive=True,
|
175 |
label="Maximum number of new tokens to generate",
|
176 |
)
|
177 |
+
repetition_penalty = gr.Slider(
|
178 |
minimum=0.01,
|
179 |
maximum=5.0,
|
180 |
value=1,
|
|
|
183 |
label="Repetition penalty",
|
184 |
info="1.0 is equivalent to no penalty",
|
185 |
)
|
186 |
+
decoding_strategy = gr.Radio(
|
187 |
[
|
188 |
"Greedy",
|
189 |
"Top P Sampling",
|
|
|
193 |
interactive=True,
|
194 |
info="Higher values are equivalent to sampling more low-probability tokens.",
|
195 |
)
|
196 |
+
temperature = gr.Slider(
|
197 |
minimum=0.0,
|
198 |
maximum=2.0,
|
199 |
value=0.5,
|
|
|
203 |
label="Sampling temperature",
|
204 |
info="Higher values will produce more diverse outputs.",
|
205 |
)
|
206 |
+
top_p = gr.Slider(
|
207 |
minimum=0.01,
|
208 |
maximum=0.99,
|
209 |
value=0.9,
|
|
|
215 |
)
|
216 |
|
217 |
# Create a chatbot interface
|
218 |
+
chatbot = gr.Chatbot(
|
219 |
label="OpenGPT-4o-Chatty",
|
220 |
show_copy_button=True,
|
221 |
likeable=True,
|
222 |
layout="panel"
|
223 |
)
|
224 |
|
225 |
+
# Define Gradio interface
|
226 |
def chat_interface(user_input, history, web_search, decoding_strategy, temperature, max_new_tokens, repetition_penalty, top_p):
|
227 |
logger.debug(f"Chat interface called with user_input: {user_input}")
|
228 |
if isinstance(user_input, str):
|
|
|
238 |
top_p,
|
239 |
tokenizer=tokenizer # Pass tokenizer to model_inference
|
240 |
)
|
241 |
+
history.append((user_input["text"], response))
|
242 |
+
logger.debug(f"Updated chat history: {history}")
|
243 |
return history, history
|
244 |
|
245 |
+
# Create Gradio interface
|
246 |
interface = gr.Interface(
|
247 |
fn=chat_interface,
|
248 |
inputs=[
|
249 |
+
gr.Textbox(label="User Input"),
|
250 |
+
gr.State([]),
|
251 |
+
gr.Checkbox(label="Web Search", value=True),
|
252 |
+
decoding_strategy,
|
253 |
+
temperature,
|
254 |
+
max_new_tokens,
|
255 |
+
repetition_penalty,
|
256 |
+
top_p
|
257 |
+
],
|
258 |
+
outputs=[
|
259 |
+
chatbot,
|
260 |
+
gr.State([])
|
261 |
],
|
|
|
262 |
title="OpenGPT-4o-Chatty",
|
263 |
+
description="An AI assistant capable of insightful conversations and web search."
|
|
|
264 |
)
|
265 |
|
266 |
if __name__ == "__main__":
|
267 |
+
logger.debug("Launching Gradio interface")
|
268 |
+
interface.launch()
|