Spaces:
Sleeping
Sleeping
James MacQuillan
commited on
Commit
·
aebf197
1
Parent(s):
388d28b
push
Browse files
app.py
CHANGED
@@ -11,63 +11,25 @@ hf_token = os.getenv("HF_TOKEN")
|
|
11 |
|
12 |
client = InferenceClient(token=hf_token)
|
13 |
|
14 |
-
custom_css = '''
|
15 |
-
.gradio-container {
|
16 |
-
font-family: 'Roboto', sans-serif;
|
17 |
-
}
|
18 |
-
.main-header {
|
19 |
-
text-align: center;
|
20 |
-
color: #4a4a4a;
|
21 |
-
margin-bottom: 2rem;
|
22 |
-
}
|
23 |
-
.tab-header {
|
24 |
-
font-size: 1.2rem;
|
25 |
-
font-weight: bold;
|
26 |
-
margin-bottom: 1rem;
|
27 |
-
}
|
28 |
-
.custom-chatbot {
|
29 |
-
border-radius: 10px;
|
30 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
31 |
-
}
|
32 |
-
.custom-button {
|
33 |
-
background-color: #3498db;
|
34 |
-
color: white;
|
35 |
-
border: none;
|
36 |
-
padding: 10px 20px;
|
37 |
-
border-radius: 5px;
|
38 |
-
cursor: pointer;
|
39 |
-
transition: background-color 0.3s ease;
|
40 |
-
}
|
41 |
-
.custom-button:hover {
|
42 |
-
background-color: #2980b9;
|
43 |
-
}
|
44 |
-
'''
|
45 |
-
|
46 |
def extract_text_from_webpage(html):
|
47 |
soup = BeautifulSoup(html, "html.parser")
|
48 |
for script in soup(["script", "style"]):
|
49 |
script.decompose()
|
50 |
-
|
51 |
-
return visible_text
|
52 |
|
53 |
def search(query):
|
54 |
-
term = query
|
55 |
-
max_chars_per_page = 8000
|
56 |
all_results = []
|
57 |
-
|
58 |
with requests.Session() as session:
|
59 |
try:
|
60 |
resp = session.get(
|
61 |
url="https://www.google.com/search",
|
62 |
-
headers={"User-Agent": "Mozilla/5.0
|
63 |
-
params={"q":
|
64 |
timeout=5
|
65 |
)
|
66 |
resp.raise_for_status()
|
67 |
-
|
68 |
soup = BeautifulSoup(resp.text, "html.parser")
|
69 |
result_block = soup.find_all("div", attrs={"class": "g"})
|
70 |
-
|
71 |
for result in result_block:
|
72 |
link = result.find("a", href=True)
|
73 |
if link:
|
@@ -75,116 +37,64 @@ def search(query):
|
|
75 |
try:
|
76 |
webpage = session.get(link, headers={"User-Agent": "Mozilla/5.0"}, timeout=5)
|
77 |
webpage.raise_for_status()
|
78 |
-
|
79 |
visible_text = extract_text_from_webpage(webpage.text)
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
except requests.exceptions.RequestException as e:
|
86 |
-
print(f"Failed to retrieve {link}: {e}")
|
87 |
-
all_results.append({"link": link, "text": None})
|
88 |
-
except requests.exceptions.RequestException as e:
|
89 |
-
print(f"Google search failed: {e}")
|
90 |
-
|
91 |
return all_results
|
92 |
|
93 |
def process_query(user_input, history):
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
# Step 1: Generate a search term based on
|
98 |
-
|
99 |
model="Qwen/Qwen2.5-72B-Instruct",
|
100 |
-
messages=
|
101 |
-
max_tokens=400
|
102 |
-
stream=True
|
103 |
)
|
104 |
|
105 |
-
|
106 |
-
search_query = ""
|
107 |
-
for chunk in stream_search:
|
108 |
-
content = chunk.choices[0].delta.content or ''
|
109 |
-
search_query += content
|
110 |
-
|
111 |
-
# Step 2: Perform the web search with the generated term
|
112 |
-
|
113 |
|
|
|
114 |
search_results = search(search_query)
|
115 |
-
|
116 |
-
# Format results as a JSON string for model input
|
117 |
search_results_str = json.dumps(search_results)
|
118 |
-
|
119 |
# Step 3: Generate a response using the search results
|
120 |
response = client.chat_completion(
|
121 |
model="Qwen/Qwen2.5-72B-Instruct",
|
122 |
-
messages=[{"role": "user", "content": f"Using the search results: {search_results_str}
|
123 |
-
max_tokens=3000
|
124 |
-
stream=True
|
125 |
)
|
126 |
|
|
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
final_response = ""
|
131 |
-
for chunk in response:
|
132 |
-
content = chunk.choices[0].delta.content or ''
|
133 |
-
final_response += content
|
134 |
-
yield final_response
|
135 |
-
|
136 |
-
theme = gr.themes.Citrus(
|
137 |
-
primary_hue="blue",
|
138 |
-
neutral_hue="slate",
|
139 |
-
)
|
140 |
-
|
141 |
-
examples = [
|
142 |
-
["whats the trending social sentiment like for Nvidia"],
|
143 |
-
["What's the latest news on Cisco Systems stock"],
|
144 |
-
["Analyze technical indicators for Adobe, are they presenting buy or sell signals"],
|
145 |
-
["Write me a smart sheet on the trending social sentiment and technical indicators for Nvidia"],
|
146 |
-
["What are the best stocks to buy this month"],
|
147 |
-
["What companies report earnings this week"],
|
148 |
-
["What's Apple's current market cap"],
|
149 |
-
["Analyze the technical indicators for Apple"],
|
150 |
-
["Build an intrinsic value model for Apple"],
|
151 |
-
["Make a table of Apple's stock price for the last 3 days"],
|
152 |
-
["What is Apple's PE ratio and how does it compare to other companies in consumer electronics"],
|
153 |
-
["How did Salesforce perform in its last earnings?"],
|
154 |
-
["What is the average analyst price target for Nvidia"],
|
155 |
-
["What is the outlook for the stock market in 2025"],
|
156 |
-
["When does Nvidia next report earnings"],
|
157 |
-
["What are the latest products from Apple"],
|
158 |
-
["What is Tesla's current price-to-earnings ratio and how does it compare to other car manufacturers?"],
|
159 |
-
["List the top 5 performing stocks in the S&P 500 this month"],
|
160 |
-
["What is the dividend yield for Coca-Cola?"],
|
161 |
-
["Which companies in the tech sector are announcing dividends this month?"],
|
162 |
-
["Analyze the latest moving averages for Microsoft; are they indicating a trend reversal?"],
|
163 |
-
["What is the latest guidance on revenue for Meta?"],
|
164 |
-
["What is the current beta of Amazon stock and how does it compare to the industry average?"],
|
165 |
-
["What are the top-rated ETFs for technology exposure this quarter?"]
|
166 |
-
]
|
167 |
-
|
168 |
|
|
|
169 |
|
170 |
with gr.Blocks(theme=theme) as demo:
|
|
|
|
|
171 |
with gr.Column():
|
172 |
gr.Markdown("## IM.S - Building the Future of Investing")
|
173 |
-
|
174 |
with gr.Column(scale=3, min_width=600):
|
175 |
chat_interface = gr.ChatInterface(
|
176 |
fn=process_query,
|
177 |
-
|
178 |
-
examples=
|
|
|
|
|
|
|
179 |
)
|
180 |
-
|
181 |
with gr.Column():
|
182 |
gr.Markdown('''
|
183 |
-
**Disclaimer**: The information provided by IM.S is for educational and informational purposes only
|
184 |
-
|
185 |
-
By using IM.S, you agree to be bound by quantineuron.com’s [Terms of Service](https://quantineuron.com/disclaimer-statement/), [Terms and Conditions](https://quantineuron.com/terms-and-conditions/), [Data Protection and Privacy Policy](https://quantineuron.com/data-protection-and-privacy-policy/), [our discalimer statement](https://quantineuron.com/disclaimer-statement/) and this Disclaimer Statement. We recommend reviewing these documents carefully. Your continued use of this service confirms your acceptance of these terms and conditions, and it is your responsibility to stay informed of any updates or changes.
|
186 |
-
|
187 |
-
**Important Note**: Investing in financial markets carries risk, and it is possible to lose some or all of the invested capital. Always consider seeking advice from a qualified financial advisor.
|
188 |
''')
|
189 |
|
190 |
demo.launch()
|
|
|
11 |
|
12 |
client = InferenceClient(token=hf_token)
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def extract_text_from_webpage(html):
|
15 |
soup = BeautifulSoup(html, "html.parser")
|
16 |
for script in soup(["script", "style"]):
|
17 |
script.decompose()
|
18 |
+
return soup.get_text(separator=" ", strip=True)
|
|
|
19 |
|
20 |
def search(query):
|
|
|
|
|
21 |
all_results = []
|
|
|
22 |
with requests.Session() as session:
|
23 |
try:
|
24 |
resp = session.get(
|
25 |
url="https://www.google.com/search",
|
26 |
+
headers={"User-Agent": "Mozilla/5.0"},
|
27 |
+
params={"q": query, "num": 7},
|
28 |
timeout=5
|
29 |
)
|
30 |
resp.raise_for_status()
|
|
|
31 |
soup = BeautifulSoup(resp.text, "html.parser")
|
32 |
result_block = soup.find_all("div", attrs={"class": "g"})
|
|
|
33 |
for result in result_block:
|
34 |
link = result.find("a", href=True)
|
35 |
if link:
|
|
|
37 |
try:
|
38 |
webpage = session.get(link, headers={"User-Agent": "Mozilla/5.0"}, timeout=5)
|
39 |
webpage.raise_for_status()
|
|
|
40 |
visible_text = extract_text_from_webpage(webpage.text)
|
41 |
+
all_results.append({"link": link, "text": visible_text[:8000]}) # Limiting text length
|
42 |
+
except requests.exceptions.RequestException:
|
43 |
+
continue
|
44 |
+
except requests.exceptions.RequestException:
|
45 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
return all_results
|
47 |
|
48 |
def process_query(user_input, history):
|
49 |
+
# Prepare chat history for context
|
50 |
+
chat_history = history + [{"role": "user", "content": user_input}]
|
51 |
+
|
52 |
+
# Step 1: Generate a search term based on user input
|
53 |
+
search_query_response = client.chat_completion(
|
54 |
model="Qwen/Qwen2.5-72B-Instruct",
|
55 |
+
messages=chat_history,
|
56 |
+
max_tokens=400
|
|
|
57 |
)
|
58 |
|
59 |
+
search_query = search_query_response.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
# Step 2: Perform the web search with the generated term
|
62 |
search_results = search(search_query)
|
|
|
|
|
63 |
search_results_str = json.dumps(search_results)
|
64 |
+
|
65 |
# Step 3: Generate a response using the search results
|
66 |
response = client.chat_completion(
|
67 |
model="Qwen/Qwen2.5-72B-Instruct",
|
68 |
+
messages=chat_history + [{"role": "user", "content": f"Using the search results: {search_results_str}, answer the user's query '{user_input}'."}],
|
69 |
+
max_tokens=3000
|
|
|
70 |
)
|
71 |
|
72 |
+
final_response = response.choices[0].message.content.strip()
|
73 |
|
74 |
+
# Return the updated chat history with the assistant's response
|
75 |
+
return history + [{"role": "user", "content": user_input}, {"role": "assistant", "content": final_response}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
theme = gr.themes.Citrus(primary_hue="blue", neutral_hue="slate")
|
78 |
|
79 |
with gr.Blocks(theme=theme) as demo:
|
80 |
+
chatbot = gr.Chatbot(type="messages", label="IM.S", avatar_images=[None, BOT_AVATAR], height=700)
|
81 |
+
|
82 |
with gr.Column():
|
83 |
gr.Markdown("## IM.S - Building the Future of Investing")
|
84 |
+
|
85 |
with gr.Column(scale=3, min_width=600):
|
86 |
chat_interface = gr.ChatInterface(
|
87 |
fn=process_query,
|
88 |
+
chatbot=chatbot,
|
89 |
+
examples=[
|
90 |
+
["What's the latest news on Apple?"],
|
91 |
+
["How is the stock market performing today?"]
|
92 |
+
]
|
93 |
)
|
94 |
+
|
95 |
with gr.Column():
|
96 |
gr.Markdown('''
|
97 |
+
**Disclaimer**: The information provided by IM.S is for educational and informational purposes only...
|
|
|
|
|
|
|
|
|
98 |
''')
|
99 |
|
100 |
demo.launch()
|