Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
import urllib
|
3 |
import requests
|
4 |
from typing import List, Dict, Union
|
|
|
5 |
import torch
|
6 |
import gradio as gr
|
7 |
from bs4 import BeautifulSoup
|
@@ -30,7 +31,7 @@ def search(term, num_results=2, lang="en", timeout=5, safe="active", ssl_verify=
|
|
30 |
start = 0
|
31 |
all_results = []
|
32 |
max_chars_per_page = 8000 # Limit the number of characters from each webpage
|
33 |
-
|
34 |
with requests.Session() as session:
|
35 |
while start < num_results:
|
36 |
resp = session.get(
|
@@ -41,6 +42,8 @@ def search(term, num_results=2, lang="en", timeout=5, safe="active", ssl_verify=
|
|
41 |
verify=ssl_verify,
|
42 |
)
|
43 |
resp.raise_for_status()
|
|
|
|
|
44 |
soup = BeautifulSoup(resp.text, "html.parser")
|
45 |
result_block = soup.find_all("div", attrs={"class": "g"})
|
46 |
if not result_block:
|
@@ -63,6 +66,7 @@ def search(term, num_results=2, lang="en", timeout=5, safe="active", ssl_verify=
|
|
63 |
else:
|
64 |
all_results.append({"link": None, "text": None})
|
65 |
start += len(result_block)
|
|
|
66 |
return all_results
|
67 |
|
68 |
# Format the prompt for the language model
|
@@ -71,10 +75,7 @@ def format_prompt(user_prompt, chat_history):
|
|
71 |
for item in chat_history:
|
72 |
if isinstance(item, tuple):
|
73 |
prompt += f"[INST] {item[0]} [/INST]"
|
74 |
-
|
75 |
-
prompt += f" {item[1]}</s>"
|
76 |
-
else:
|
77 |
-
prompt += "</s>"
|
78 |
else:
|
79 |
prompt += f" [Image] "
|
80 |
prompt += f"[INST] {user_prompt} [/INST]"
|
@@ -82,21 +83,17 @@ def format_prompt(user_prompt, chat_history):
|
|
82 |
|
83 |
# Model inference function
|
84 |
def start_inference(prompt, enable_web_search):
|
85 |
-
logging.debug("Starting inference")
|
86 |
return next(model_inference(prompt, enable_web_search))
|
87 |
|
88 |
def model_inference(prompt, enable_web_search):
|
89 |
-
logging.debug(f"Model inference with prompt: {prompt}, enable_web_search: {enable_web_search}")
|
90 |
for response in fetch_response(prompt, enable_web_search):
|
91 |
yield response
|
92 |
|
93 |
def fetch_response(prompt, enable_web_search):
|
94 |
-
logging.debug(f"Fetching response with enable_web_search: {enable_web_search}")
|
95 |
if enable_web_search:
|
96 |
# Perform web search and generate text based on the retrieved results
|
97 |
web_results = search(prompt)
|
98 |
web2 = ' '.join([f"Link: {res['link']}\nText: {res['text']}\n\n" for res in web_results])
|
99 |
-
logging.debug(f"Web search results: {web2}")
|
100 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
101 |
generate_kwargs = dict(max_new_tokens=4000, do_sample=True)
|
102 |
formatted_prompt = format_prompt(
|
@@ -107,7 +104,6 @@ def fetch_response(prompt, enable_web_search):
|
|
107 |
for response in stream:
|
108 |
if not response.token.text == "</s>":
|
109 |
output += response.token.text
|
110 |
-
logging.debug(f"Streaming response: {response.token.text}")
|
111 |
yield output
|
112 |
else:
|
113 |
# Use the microsoft/Phi-3-mini-4k-instruct model for generating text based on user prompts
|
@@ -119,7 +115,6 @@ def fetch_response(prompt, enable_web_search):
|
|
119 |
for response in stream:
|
120 |
if not response.token.text == "</s>":
|
121 |
output += response.token.text
|
122 |
-
logging.debug(f"Streaming response: {response.token.text}")
|
123 |
yield output
|
124 |
|
125 |
# Create a chatbot interface with a Fetch button
|
@@ -127,7 +122,7 @@ chatbot = gr.Interface(
|
|
127 |
fn=start_inference,
|
128 |
inputs=[
|
129 |
gr.Textbox(label="User Prompt", placeholder="Enter your prompt here..."),
|
130 |
-
gr.Checkbox(label="Enable Web Search", value=False,
|
131 |
],
|
132 |
outputs=gr.Textbox(label="Response", placeholder="Responses will appear here..."),
|
133 |
live=True
|
|
|
2 |
import urllib
|
3 |
import requests
|
4 |
from typing import List, Dict, Union
|
5 |
+
from threading import Thread
|
6 |
import torch
|
7 |
import gradio as gr
|
8 |
from bs4 import BeautifulSoup
|
|
|
31 |
start = 0
|
32 |
all_results = []
|
33 |
max_chars_per_page = 8000 # Limit the number of characters from each webpage
|
34 |
+
|
35 |
with requests.Session() as session:
|
36 |
while start < num_results:
|
37 |
resp = session.get(
|
|
|
42 |
verify=ssl_verify,
|
43 |
)
|
44 |
resp.raise_for_status()
|
45 |
+
logging.debug(f"Raw HTML response from Google: {resp.text[:500]}") # Log the first 500 characters of the HTML
|
46 |
+
|
47 |
soup = BeautifulSoup(resp.text, "html.parser")
|
48 |
result_block = soup.find_all("div", attrs={"class": "g"})
|
49 |
if not result_block:
|
|
|
66 |
else:
|
67 |
all_results.append({"link": None, "text": None})
|
68 |
start += len(result_block)
|
69 |
+
logging.debug(f"Web search results: {all_results}")
|
70 |
return all_results
|
71 |
|
72 |
# Format the prompt for the language model
|
|
|
75 |
for item in chat_history:
|
76 |
if isinstance(item, tuple):
|
77 |
prompt += f"[INST] {item[0]} [/INST]"
|
78 |
+
prompt += f" {item[1]}</s>"
|
|
|
|
|
|
|
79 |
else:
|
80 |
prompt += f" [Image] "
|
81 |
prompt += f"[INST] {user_prompt} [/INST]"
|
|
|
83 |
|
84 |
# Model inference function
|
85 |
def start_inference(prompt, enable_web_search):
|
|
|
86 |
return next(model_inference(prompt, enable_web_search))
|
87 |
|
88 |
def model_inference(prompt, enable_web_search):
|
|
|
89 |
for response in fetch_response(prompt, enable_web_search):
|
90 |
yield response
|
91 |
|
92 |
def fetch_response(prompt, enable_web_search):
|
|
|
93 |
if enable_web_search:
|
94 |
# Perform web search and generate text based on the retrieved results
|
95 |
web_results = search(prompt)
|
96 |
web2 = ' '.join([f"Link: {res['link']}\nText: {res['text']}\n\n" for res in web_results])
|
|
|
97 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
98 |
generate_kwargs = dict(max_new_tokens=4000, do_sample=True)
|
99 |
formatted_prompt = format_prompt(
|
|
|
104 |
for response in stream:
|
105 |
if not response.token.text == "</s>":
|
106 |
output += response.token.text
|
|
|
107 |
yield output
|
108 |
else:
|
109 |
# Use the microsoft/Phi-3-mini-4k-instruct model for generating text based on user prompts
|
|
|
115 |
for response in stream:
|
116 |
if not response.token.text == "</s>":
|
117 |
output += response.token.text
|
|
|
118 |
yield output
|
119 |
|
120 |
# Create a chatbot interface with a Fetch button
|
|
|
122 |
fn=start_inference,
|
123 |
inputs=[
|
124 |
gr.Textbox(label="User Prompt", placeholder="Enter your prompt here..."),
|
125 |
+
gr.Checkbox(label="Enable Web Search", value=False, description="Search Web")
|
126 |
],
|
127 |
outputs=gr.Textbox(label="Response", placeholder="Responses will appear here..."),
|
128 |
live=True
|