import gradio as gr import os import json from bs4 import BeautifulSoup import requests from huggingface_hub import InferenceClient # Define global variables BOT_AVATAR = 'https://automatedstockmining.org/wp-content/uploads/2024/08/south-west-value-mining-logo.webp' hf_token = os.getenv("HF_TOKEN") client = InferenceClient(token=hf_token) custom_css = ''' .gradio-container { font-family: 'Roboto', sans-serif; } .main-header { text-align: center; color: #4a4a4a; margin-bottom: 2rem; } .tab-header { font-size: 1.2rem; font-weight: bold; margin-bottom: 1rem; } .custom-chatbot { border-radius: 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); } .custom-button { background-color: #3498db; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease; } .custom-button:hover { background-color: #2980b9; } ''' def extract_text_from_webpage(html): soup = BeautifulSoup(html, "html.parser") for script in soup(["script", "style"]): script.decompose() visible_text = soup.get_text(separator=" ", strip=True) return visible_text def search(query): term = query max_chars_per_page = 8000 all_results = [] with requests.Session() as session: try: resp = session.get( url="https://www.google.com/search", headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0"}, params={"q": term, "num": 7}, timeout=5 ) resp.raise_for_status() soup = BeautifulSoup(resp.text, "html.parser") result_block = soup.find_all("div", attrs={"class": "g"}) for result in result_block: link = result.find("a", href=True) if link: link = link["href"] try: webpage = session.get(link, headers={"User-Agent": "Mozilla/5.0"}, timeout=5) webpage.raise_for_status() visible_text = extract_text_from_webpage(webpage.text) if len(visible_text) > max_chars_per_page: visible_text = visible_text[:max_chars_per_page] all_results.append({"link": link, "text": visible_text}) except requests.exceptions.RequestException as e: print(f"Failed to retrieve {link}: {e}") all_results.append({"link": link, "text": None}) except requests.exceptions.RequestException as e: print(f"Google search failed: {e}") return all_results def process_query(user_input, history): # Step 1: Generate a search term based on the user query stream_search = client.chat_completion( model="Qwen/Qwen2.5-72B-Instruct", messages=[{"role": "user", "content": f"Based on this chat history {history} and the user's request '{user_input}', suggest a Google search term in a single line without specific dates; use 'this year', 'this month', etc. INCLUDE NOTHING IN YOUR RESPONSE EXCEPT THE RELEVANT SEARCH RESULT. EXAMPLE: USER: WHAT IS THE CURRENT PRICE OF COCA COLA STOCK. YOUR RESPONSE: WHAT IS THE CURRENT PRICE OF COCA COLA STOCK"}], max_tokens=400, stream=True ) # Collect the search term search_query = "" for chunk in stream_search: content = chunk.choices[0].delta.content or '' search_query += content # Step 2: Perform the web search with the generated term search_results = search(search_query) # Format results as a JSON string for model input search_results_str = json.dumps(search_results) # Step 3: Generate a response using the search results response = client.chat_completion( model="Qwen/Qwen2.5-72B-Instruct", messages=[{"role": "user", "content": f"Using the search results: {search_results_str} and chat history {history}, answer the user's query '{user_input}' concisely, using numerical data if available. if they are just making conversation, respond normally, without the data "}], max_tokens=3000, stream=True ) # Stream final response final_response = "" for chunk in response: content = chunk.choices[0].delta.content or '' final_response += content yield final_response theme = gr.themes.Citrus( primary_hue="blue", neutral_hue="slate", ) examples = [ ["whats the trending social sentiment like for Nvidia"] ] chatbot = gr.Chatbot( label="IM.analyst", avatar_images=[None, BOT_AVATAR], show_copy_button=True, layout="panel", height=700 ) with gr.Blocks(theme=theme) as demo: with gr.Column(): gr.Markdown("## IM.S - Building the Future of Investing") with gr.Column(min_width = 900,scale = 4): chat_interface = gr.ChatInterface( fn=process_query, chatbot = chatbot, examples=examples ) with gr.Column(): gr.Markdown(''' **Disclaimer**: The information provided by IM.S is for educational and informational purposes only and does not constitute financial, investment, or professional advice. By using this service, you acknowledge and agree that all decisions you make based on the information provided are made at your own risk. Neither IM.S nor quantineuron.com is liable for any financial losses or damages resulting from reliance on information provided by this chatbot. 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. **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. ''') demo.launch()