James MacQuillan commited on
Commit
0401525
·
1 Parent(s): 6d99a3a
Files changed (1) hide show
  1. app.py +59 -95
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  from typing import final
3
  import gradio as gr
4
  import os
@@ -6,35 +5,32 @@ import json
6
  from bs4 import BeautifulSoup
7
  import requests
8
  from huggingface_hub import InferenceClient
9
- from datetime import datetime, timedelta
10
- import json
11
-
12
 
13
  # Define global variables
14
  BOT_AVATAR = 'https://automatedstockmining.org/wp-content/uploads/2024/08/south-west-value-mining-logo.webp'
15
- client = InferenceClient(token=os.getenv("HF_TOKEN"))
 
 
 
16
  custom_css = '''
17
  .gradio-container {
18
  font-family: 'Roboto', sans-serif;
19
  }
20
-
21
  .main-header {
22
  text-align: center;
23
  color: #4a4a4a;
24
  margin-bottom: 2rem;
25
  }
26
-
27
  .tab-header {
28
  font-size: 1.2rem;
29
  font-weight: bold;
30
  margin-bottom: 1rem;
31
  }
32
-
33
  .custom-chatbot {
34
  border-radius: 10px;
35
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
36
  }
37
-
38
  .custom-button {
39
  background-color: #3498db;
40
  color: white;
@@ -44,16 +40,13 @@ custom_css = '''
44
  cursor: pointer;
45
  transition: background-color 0.3s ease;
46
  }
47
-
48
  .custom-button:hover {
49
  background-color: #2980b9;
50
  }
51
  '''
52
 
53
-
54
  def extract_text_from_webpage(html):
55
  soup = BeautifulSoup(html, "html.parser")
56
- # Extract visible text, removing unnecessary elements (e.g., scripts, styles)
57
  for script in soup(["script", "style"]):
58
  script.decompose()
59
  visible_text = soup.get_text(separator=" ", strip=True)
@@ -66,14 +59,13 @@ def search(query):
66
 
67
  with requests.Session() as session:
68
  try:
69
- # Send a search request to Google
70
  resp = session.get(
71
  url="https://www.google.com/search",
72
  headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"},
73
  params={"q": term, "num": 4},
74
  timeout=5
75
  )
76
- resp.raise_for_status() # Ensure the request was successful
77
 
78
  soup = BeautifulSoup(resp.text, "html.parser")
79
  result_block = soup.find_all("div", attrs={"class": "g"})
@@ -83,11 +75,9 @@ def search(query):
83
  if link:
84
  link = link["href"]
85
  try:
86
- # Fetch the webpage at the found link
87
  webpage = session.get(link, headers={"User-Agent": "Mozilla/5.0"}, timeout=5)
88
  webpage.raise_for_status()
89
 
90
- # Extract visible text from the webpage
91
  visible_text = extract_text_from_webpage(webpage.text)
92
  if len(visible_text) > max_chars_per_page:
93
  visible_text = visible_text[:max_chars_per_page]
@@ -101,89 +91,63 @@ def search(query):
101
  print(f"Google search failed: {e}")
102
 
103
  return all_results
104
- def process_query(user_input, history):
105
- if len(history) > 0 and history[-1]['role'] == 'user' and history[-1]['content'].lower() == user_input.lower():
106
- gr.Info('Searching the web for the latest data...', duration=4)
107
- else:
108
- # Append new user message to the history
109
- history.append({"role": "user", "content": user_input})
110
- gr.Info('thinking...',duration = 7)
111
- search_results = search(user_input)
112
- search_results_str = json.dumps(search_results)
113
- response = client.chat_completion(
114
- model="Qwen/Qwen2.5-72B-Instruct",
115
- messages=[{"role": "user", "content": f"YOU ARE IM.X, AN INVESTMENT CHATBOT BUILT BY automatedstockmining.org. Answer the user's request '{user_input}' using the following information: {search_results_str} and the chat history{history}. Provide a concise, direct answer in no more than 2-3 sentences. use the appropriate emojis for some of your responses"}],
116
- max_tokens=400,
117
- stream=False
118
- )
119
- final_response = response.choices[0].message['content']
120
- history.append({"role": "assistant", "content": final_response})
121
-
122
- return history, "" # Clear the input box after sending the response
123
-
124
-
125
- def clear_history():
126
- return [], "" # Return empty history and clear the input box
127
-
128
- # Function to undo the last user-bot message pair
129
- def undo_last(history):
130
- if len(history) >= 2: # Ensure that there's at least one user-bot message pair
131
- history.pop() # Remove the bot's response
132
- history.pop() # Remove the user's input
133
- return history, "" # Return updated history and clear the input box
134
-
135
- # Gradio UI setup
136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  theme = gr.themes.Citrus(
139
  primary_hue="blue",
140
  neutral_hue="slate",
141
  )
142
 
143
-
144
- with gr.Blocks(theme = theme,css = custom_css) as demo:
145
- gr.Markdown("<h1 style='text-align: center;'>IM.S - built for speed: Intelligent Investing with automatedstockmining.org</h1>")
146
-
147
- with gr.Column():
148
- chatbot_display = gr.Chatbot(label="IM.B chat", avatar_images=[None, BOT_AVATAR], height=600, type="messages", layout="panel")
149
-
150
- # User input and send button
151
- with gr.Row():
152
- user_input = gr.Textbox(placeholder="Type your message here...", label=None, show_label=False)
153
- send_button = gr.Button("Send")
154
-
155
- example_inputs = [
156
- ["What's the current price of bitcoin"],
157
- ["What's the latest news on Cisco Systems stock"],
158
- ["Analyze technical indicators for Adobe, are they presenting buy or sell signals"],
159
- ["What's the current price of Apple stock"],
160
- ["What are the best stocks to buy this month"],
161
- ['what companies report earnings this week'],
162
- ["whats apple's current market cap"]
163
- ]
164
-
165
- # Add examples to the UI
166
- gr.Examples(examples=example_inputs, inputs=user_input)
167
-
168
- # Buttons for Clear and Undo
169
- with gr.Row():
170
- clear_button = gr.Button("Clear Chat")
171
- undo_button = gr.Button("Undo Last")
172
-
173
- # History is now handled as part of the chatbot_display
174
- send_button.click(process_query, inputs=[user_input, chatbot_display], outputs=[chatbot_display, user_input])
175
-
176
- # Allow pressing Enter to submit the input
177
- user_input.submit(process_query, inputs=[user_input, chatbot_display], outputs=[chatbot_display, user_input])
178
-
179
- # Action for Clear Button
180
- clear_button.click(clear_history, outputs=[chatbot_display, user_input])
181
-
182
- # Action for Undo Button
183
- undo_button.click(undo_last, inputs=[chatbot_display], outputs=[chatbot_display, user_input])
184
-
185
- # Launch the Gradio app
186
- demo.launch()
187
-
188
-
189
-
 
 
1
  from typing import final
2
  import gradio as gr
3
  import os
 
5
  from bs4 import BeautifulSoup
6
  import requests
7
  from huggingface_hub import InferenceClient
8
+ from google.colab import userdata
 
 
9
 
10
  # Define global variables
11
  BOT_AVATAR = 'https://automatedstockmining.org/wp-content/uploads/2024/08/south-west-value-mining-logo.webp'
12
+ hf_token = userdata.get("HF_TOKEN")
13
+
14
+ client = InferenceClient(token=hf_token)
15
+
16
  custom_css = '''
17
  .gradio-container {
18
  font-family: 'Roboto', sans-serif;
19
  }
 
20
  .main-header {
21
  text-align: center;
22
  color: #4a4a4a;
23
  margin-bottom: 2rem;
24
  }
 
25
  .tab-header {
26
  font-size: 1.2rem;
27
  font-weight: bold;
28
  margin-bottom: 1rem;
29
  }
 
30
  .custom-chatbot {
31
  border-radius: 10px;
32
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
33
  }
 
34
  .custom-button {
35
  background-color: #3498db;
36
  color: white;
 
40
  cursor: pointer;
41
  transition: background-color 0.3s ease;
42
  }
 
43
  .custom-button:hover {
44
  background-color: #2980b9;
45
  }
46
  '''
47
 
 
48
  def extract_text_from_webpage(html):
49
  soup = BeautifulSoup(html, "html.parser")
 
50
  for script in soup(["script", "style"]):
51
  script.decompose()
52
  visible_text = soup.get_text(separator=" ", strip=True)
 
59
 
60
  with requests.Session() as session:
61
  try:
 
62
  resp = session.get(
63
  url="https://www.google.com/search",
64
  headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"},
65
  params={"q": term, "num": 4},
66
  timeout=5
67
  )
68
+ resp.raise_for_status()
69
 
70
  soup = BeautifulSoup(resp.text, "html.parser")
71
  result_block = soup.find_all("div", attrs={"class": "g"})
 
75
  if link:
76
  link = link["href"]
77
  try:
 
78
  webpage = session.get(link, headers={"User-Agent": "Mozilla/5.0"}, timeout=5)
79
  webpage.raise_for_status()
80
 
 
81
  visible_text = extract_text_from_webpage(webpage.text)
82
  if len(visible_text) > max_chars_per_page:
83
  visible_text = visible_text[:max_chars_per_page]
 
91
  print(f"Google search failed: {e}")
92
 
93
  return all_results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
+ def process_query(user_input, history):
96
+ gr.Info('ℹ️ thinking...',duration = 6)
97
+ # Start with a system message
98
+ messages = [{'role': 'system', 'content': "YOU ARE IM.X, AN INVESTMENT CHATBOT BUILT BY automatedstockmining.org. "}]
99
+
100
+ # Append history to messages
101
+ for user, assistant in history:
102
+ messages.append({'role': 'user', 'content': user})
103
+ messages.append({'role': 'assistant', 'content': assistant})
104
+ messages.append({'role': 'user', 'content': user_input})
105
+
106
+ # Perform the web search based on user input
107
+ search_results = search(user_input)
108
+ search_results_str = json.dumps(search_results)
109
+
110
+
111
+ # Create completion request to HuggingFace client
112
+ response = client.chat_completion(
113
+ model="Qwen/Qwen2.5-72B-Instruct",
114
+ messages=[{"role": "user", "content": f"YOU ARE IM.X, AN INVESTMENT CHATBOT BUILT BY automatedstockmining.org. Answer the user's request '{user_input}' using the following information: {search_results_str} and the chat history{history}. Provide a concise, direct answer in no more than 2-3 sentences. use the appropriate emojis for some of your responses"}],
115
+ max_tokens=400,
116
+ stream=True
117
+ )
118
+
119
+ final_response = ""
120
+ for chunk in response:
121
+ content = chunk.choices[0].delta.content or ''
122
+ final_response += content
123
+ yield final_response # Yield the accumulated response for real-time streaming
124
 
125
  theme = gr.themes.Citrus(
126
  primary_hue="blue",
127
  neutral_hue="slate",
128
  )
129
 
130
+ examples = [
131
+ ["What's the current price of bitcoin"],
132
+ ["What's the latest news on Cisco Systems stock"],
133
+ ["Analyze technical indicators for Adobe, are they presenting buy or sell signals"],
134
+ ["What's the current price of Apple stock"],
135
+ ["What are the best stocks to buy this month"],
136
+ ["What companies report earnings this week"],
137
+ ["What's Apple's current market cap"]
138
+ ]
139
+
140
+ chatbot = gr.Chatbot(
141
+ label="IM.S",
142
+ avatar_images=[None, BOT_AVATAR],
143
+ show_copy_button=True,
144
+ layout="panel",
145
+ height = 600
146
+
147
+ )
148
+ gr.ChatInterface(
149
+ theme = theme,
150
+ fn=process_query,
151
+ chatbot=chatbot,
152
+ examples=examples,
153
+ ).launch()