Spaces:
Sleeping
Sleeping
File size: 12,843 Bytes
2d5a663 e3e85f6 7979036 2d5a663 b8c3f18 e3e85f6 b8c3f18 8d6418d b8c3f18 e3e85f6 b8c3f18 2d5a663 ad93408 b8c3f18 e3e85f6 8d6418d 2d5a663 b8c3f18 e3e85f6 6692892 e3e85f6 ad93408 e3e85f6 ad93408 b8c3f18 ad93408 b8c3f18 ad93408 e3e85f6 ad93408 e3e85f6 ad93408 e3e85f6 b8c3f18 894c97b e3e85f6 b8c3f18 18460d0 b8c3f18 2d5a663 ad93408 e3e85f6 b8c3f18 2d5a663 b8c3f18 7979036 2d5a663 8d6418d 2d5a663 b8c3f18 2d5a663 8d6418d 2d5a663 8d6418d 2d5a663 b8c3f18 2d5a663 b8c3f18 2d5a663 8d6418d 2d5a663 b8c3f18 4314e45 6692892 8d6418d 6692892 8d6418d 4314e45 75b7587 ad93408 75b7587 ad93408 75b7587 2d5a663 8d6418d 2d5a663 8d6418d 2d5a663 18460d0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | import gradio as gr
import numpy as np
import inspect
import pandas as pd
import torch
import re
from datasets import load_dataset
from sentence_transformers import SentenceTransformer
import faiss
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM
import spaces # <--- Import the Hugging Face spaces library for Free GPU
# =========================================================================
# 1. LAZY LOADING ARCHITECTURE (The Ultimate Fix for Smart AI)
# To get a truly "smart" pitch, we MUST use a modern State-of-the-Art LLM.
# We are upgrading from the ancient 'flan-t5' to the brilliant 'Qwen2.5' model.
# By lazy loading, we ensure Hugging Face never crashes during startup!
# =========================================================================
# Load the dataset globally so the UI Dropdown knows what sectors exist
print("Loading Dataset...")
df = pd.DataFrame(load_dataset('Yoel125/synthetic-companies-12k', split='train'))
df['full_text'] = df['sector'] + " - " + df['theme'] + ": " + df['description']
all_sectors = ["All Sectors"] + sorted(list(df['sector'].unique()))
# Global caches for the heavy AI models
embedding_model_cache = None
faiss_index_cache = None
gen_tokenizer_cache = None
gen_model_cache = None
def get_ai_models():
global embedding_model_cache, faiss_index_cache, gen_tokenizer_cache, gen_model_cache
if embedding_model_cache is None:
print("Lazy-loading Embedding Model...")
embedding_model_cache = SentenceTransformer('paraphrase-MiniLM-L3-v2', device='cpu')
if faiss_index_cache is None:
try:
print("Loading FAISS index...")
embeddings = np.load('company_embeddings.npy')
faiss_index_cache = faiss.IndexFlatL2(embeddings.shape[1])
faiss_index_cache.add(np.array(embeddings).astype('float32'))
except FileNotFoundError:
print("Generating new FAISS index...")
embeddings = embedding_model_cache.encode(df['full_text'].tolist(), show_progress_bar=False)
faiss_index_cache = faiss.IndexFlatL2(embeddings.shape[1])
faiss_index_cache.add(np.array(embeddings).astype('float32'))
if gen_model_cache is None:
print("Lazy-loading State-of-the-Art GenAI Model (Qwen2.5-0.5B-Instruct)...")
# UPGRADED to a massively smarter, modern Causal LLM (ChatGPT equivalent for small models)
gen_tokenizer_cache = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
gen_model_cache = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct", torch_dtype="auto")
gen_model_cache.eval()
return embedding_model_cache, faiss_index_cache, gen_tokenizer_cache, gen_model_cache
def generate_sales_pitch(user_query, company_name, sector, theme, description, tokenizer, model):
device = "cuda" if torch.cuda.is_available() else "cpu"
# Modern ChatML format used by state-of-the-art models like Qwen and Llama
messages = [
{"role": "system", "content": "You are a brilliant, aggressive Wall Street investment analyst. Your job is to write a single, highly persuasive, creative sentence explaining why a company is a massive investment opportunity."},
{"role": "user", "content": f"Company: {company_name}\nIndustry: {theme}\nWhat they do: {description[:300]}\n\nWrite a 1-sentence sales pitch explaining why this company is the ultimate strategic investment for someone focused on '{user_query}'. Do not just summarize what they do. Be creative and aggressive."}
]
# Apply the exact chat template the model was trained on
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
try:
model.to(device)
inputs = tokenizer(prompt, return_tensors="pt").to(device)
# 100% STRICT DETERMINISM: Forces the exact same brilliant output every time!
torch.manual_seed(42)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(42)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=75,
do_sample=False,
repetition_penalty=1.1,
)
# Causal LMs output the prompt + generation. We slice off the prompt.
input_length = inputs.input_ids.shape[1]
pitch = tokenizer.decode(outputs[0][input_length:], skip_special_tokens=True).strip()
# Clean up quotes
if pitch and pitch[0] == '"' and pitch[-1] == '"':
pitch = pitch[1:-1]
# Just in case the AI gets chatty, force it to 1-2 sentences max
sentences = pitch.split(". ")
if len(sentences) > 2:
pitch = ". ".join(sentences[:2]) + "."
if pitch:
pitch = pitch[0].upper() + pitch[1:]
return pitch
except Exception as e:
print(f"GenAI Generation Error: {e}")
return f"By leveraging their advanced {theme} capabilities, {company_name} is perfectly positioned to capture explosive growth and completely dominate the '{user_query}' space."
# --- 2. RECOMMENDATION ENGINE LOGIC ---
@spaces.GPU # <--- Hugging Face ZeroGPU Decorator!
def recommend_investment(user_query, selected_sector, top_k=3):
if (not user_query or not str(user_query).strip()) and selected_sector == "All Sectors":
yield "Please enter an investment thesis or keyword in the text box above, or select a specific industry sector from the dropdown menu."
return
if user_query and str(user_query).strip():
non_english_chars = sum(1 for char in str(user_query) if ord(char) > 127)
if non_english_chars > 2:
yield "β οΈ **Language Not Supported:** SectorSync AI is currently optimized exclusively for English data. Please write your investment thesis in English and try again."
return
if not user_query or not str(user_query).strip():
user_query = f"innovative {selected_sector} companies"
# Streaming a status message so you know why the first click takes a few seconds!
yield "π Initializing Smart AI Engine... (The very first search takes ~10 seconds to load the heavy AI. Future searches will be instant!)"
# Boot up the heavy AI models safely!
embedding_model, faiss_index, gen_tokenizer, gen_model = get_ai_models()
yield "π Searching for matching companies and writing smart pitches..."
if selected_sector != "All Sectors":
enriched_query = f"{selected_sector} industry B2B company specializing in: {user_query}"
else:
enriched_query = f"B2B investment opportunity specializing in: {user_query}"
query_vector = embedding_model.encode([enriched_query])
search_k = 25 if selected_sector != "All Sectors" else top_k
distances, indices = faiss_index.search(np.array(query_vector).astype('float32'), k=search_k)
matches = []
for i in range(search_k):
idx = indices[0][i]
sim_score = 1 / (1 + distances[0][i])
company_row = df.iloc[idx]
if selected_sector != "All Sectors" and company_row['sector'] != selected_sector:
continue
matches.append((company_row, sim_score))
if len(matches) == top_k:
break
if not matches:
for i in range(min(top_k, len(indices[0]))):
idx = indices[0][i]
sim_score = 1 / (1 + distances[0][i])
matches.append((df.iloc[idx], sim_score))
output_markdown = f"### Top {len(matches)} AI-Recommended Matches for: *'{user_query}'*\n\n"
if selected_sector != "All Sectors":
output_markdown += f"**Filtered by Sector:** `{selected_sector}`\n\n---\n\n"
else:
output_markdown += "---\n\n"
match_labels = ["π₯ Strongest Match", "π₯ Close Match", "π₯ Close Match"]
for rank, (row, score) in enumerate(matches, 1):
c_name = row['company_name']
ticker = row.get('ticker', 'N/A')
sector = row['sector']
theme = row['theme']
desc = row['description']
try:
pitch = generate_sales_pitch(user_query, c_name, sector, theme, desc, gen_tokenizer, gen_model)
except Exception:
pitch = f"By leveraging their advanced {theme} capabilities, {c_name} is perfectly positioned to capture explosive growth and completely dominate the '{user_query}' space."
tier_label = match_labels[rank - 1] if rank <= len(match_labels) else "Match"
output_markdown += f"#### #{rank}. {c_name} (`{ticker}`) β *{sector}*\n\n"
output_markdown += f"**{tier_label}** (Similarity Score: `{score*100:.1f}%`)\n\n"
output_markdown += f"**Industry:** {theme}\n\n"
output_markdown += f"**GenAI Investment Pitch:** *'{pitch}'*\n\n"
output_markdown += f"**Company Overview:** {desc[:200]}...\n\n---\n\n"
yield output_markdown
# --- 3. GRADIO USER INTERFACE ---
custom_theme = gr.themes.Base(
primary_hue="emerald",
neutral_hue="slate"
).set(
body_background_fill="#121212",
body_background_fill_dark="#121212",
body_text_color="#f0f4f8",
body_text_color_dark="#f0f4f8",
background_fill_primary="#1e1e1e",
background_fill_primary_dark="#1e1e1e",
background_fill_secondary="#121212",
background_fill_secondary_dark="#121212",
border_color_primary="#333333",
border_color_primary_dark="#333333",
block_background_fill="#1e1e1e",
block_background_fill_dark="#1e1e1e",
block_label_text_color="#f0f4f8",
block_label_text_color_dark="#f0f4f8",
input_background_fill="#2a2a2a",
input_background_fill_dark="#2a2a2a",
button_primary_background_fill="#10b981",
button_primary_background_fill_dark="#10b981",
button_primary_text_color="#121212",
button_primary_text_color_dark="#121212",
table_even_background_fill="#1e1e1e",
table_even_background_fill_dark="#1e1e1e",
table_odd_background_fill="#121212",
table_odd_background_fill_dark="#121212",
table_border_color="#333333",
table_border_color_dark="#333333"
)
# THE ULTIMATE CSS FIX for the invisible text!
# This ensures that no matter what mode the OS is in, the little boxes
# holding the Ticker and Similarity Score are forced to have a dark background
# and a bright emerald green text color so they pop out beautifully.
custom_css = """
table, table.dataset, tbody, thead, tr, td, th {
background-color: #1e1e1e !important;
color: #f0f4f8 !important;
border-color: #333333 !important;
}
code, pre {
background-color: #2a2a2a !important;
color: #10b981 !important;
border: 1px solid #333333 !important;
padding: 2px 6px !important;
border-radius: 4px !important;
}
"""
with gr.Blocks(theme=custom_theme, css=custom_css, title="SectorSync AI") as demo:
gr.Markdown("# SectorSync AI")
gr.Markdown("Cut Through the Market Noise β Find Your Next Winning Stock in Seconds. Discover high-growth companies matching your investment thesis using FAISS Vector Search and Generative AI.")
gr.Markdown("*Similarity Score reflects how closely a company profile matches your query in AI-embedding space β a relative ranking signal, not a calibrated financial confidence rating.*")
with gr.Row():
with gr.Column(scale=2):
query_input = gr.Textbox(
label="What is your investment thesis or topic?",
placeholder="e.g., autonomous robotics, clean battery storage, gene therapy for rare diseases...",
lines=2
)
with gr.Column(scale=1):
sector_dropdown = gr.Dropdown(
choices=all_sectors,
value="All Sectors",
label="Sector"
)
search_button = gr.Button("Find Investment Matches", variant="primary")
gr.Examples(
examples=[
["Artificial Intelligence and Machine Learning in Healthcare", "Healthcare"],
["Next-generation Renewable Energy and Solar Battery Storage", "Energy"],
["Autonomous Robotics and Supply Chain Logistics Automation", "Industrials"]
],
inputs=[query_input, sector_dropdown],
label="Quick Starters (1-Click Example Searches)"
)
results_output = gr.Markdown(label="Recommendation Results")
search_button.click(fn=recommend_investment, inputs=[query_input, sector_dropdown], outputs=results_output)
query_input.submit(fn=recommend_investment, inputs=[query_input, sector_dropdown], outputs=results_output)
if __name__ == "__main__":
print("Launching SectorSync AI Recommender App...")
demo.launch(share=False) |