Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,14 +11,13 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
| 11 |
import spaces # <--- Import the Hugging Face spaces library for Free GPU
|
| 12 |
|
| 13 |
# --- 1. LOAD GENERATIVE AI COMPONENT ---
|
| 14 |
-
print("Loading GenAI Component
|
| 15 |
|
| 16 |
-
#
|
| 17 |
gen_tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 18 |
gen_model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
|
| 19 |
|
| 20 |
def generate_sales_pitch(user_query, company_name, sector, theme, description):
|
| 21 |
-
# Feeding the company's description to the AI so it knows exactly what it's selling
|
| 22 |
prompt = (
|
| 23 |
f"Based on this company description: '{description[:300]}'\n\n"
|
| 24 |
f"Write one single, highly persuasive sentence explaining why investing in {company_name} "
|
|
@@ -26,7 +25,7 @@ def generate_sales_pitch(user_query, company_name, sector, theme, description):
|
|
| 26 |
)
|
| 27 |
|
| 28 |
try:
|
| 29 |
-
#
|
| 30 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 31 |
gen_model.to(device)
|
| 32 |
inputs = gen_tokenizer(prompt, return_tensors="pt", max_length=256, truncation=True).to(device)
|
|
@@ -57,8 +56,9 @@ if 'df' not in globals():
|
|
| 57 |
df['full_text'] = df['sector'] + " - " + df['theme'] + ": " + df['description']
|
| 58 |
|
| 59 |
if 'embedding_model' not in globals():
|
| 60 |
-
print("Auto-loading embedding model 'paraphrase-MiniLM-L3-v2'...")
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
if 'faiss_index' not in globals():
|
| 64 |
try:
|
|
@@ -67,7 +67,7 @@ if 'faiss_index' not in globals():
|
|
| 67 |
faiss_index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 68 |
faiss_index.add(np.array(embeddings).astype('float32'))
|
| 69 |
except FileNotFoundError:
|
| 70 |
-
print("Saved embeddings not
|
| 71 |
embeddings = embedding_model.encode(df['full_text'].tolist(), show_progress_bar=False)
|
| 72 |
faiss_index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 73 |
faiss_index.add(np.array(embeddings).astype('float32'))
|
|
@@ -145,14 +145,12 @@ def recommend_investment(user_query, selected_sector, top_k=3):
|
|
| 145 |
|
| 146 |
tier_label = match_labels[rank - 1] if rank <= len(match_labels) else "Match"
|
| 147 |
|
| 148 |
-
# The Fix: No bullet points, reads like a clean, professional report!
|
| 149 |
output_markdown += f"#### #{rank}. {c_name} (`{ticker}`) — *{sector}*\n\n"
|
| 150 |
output_markdown += f"**{tier_label}** (Similarity Score: `{score*100:.1f}%`)\n\n"
|
| 151 |
output_markdown += f"**Industry:** {theme}\n\n"
|
| 152 |
output_markdown += f"**GenAI Investment Pitch:** *'{pitch}'*\n\n"
|
| 153 |
output_markdown += f"**Company Overview:** {desc[:200]}...\n\n---\n\n"
|
| 154 |
|
| 155 |
-
# Streams the results instantly!
|
| 156 |
yield output_markdown
|
| 157 |
|
| 158 |
|
|
|
|
| 11 |
import spaces # <--- Import the Hugging Face spaces library for Free GPU
|
| 12 |
|
| 13 |
# --- 1. LOAD GENERATIVE AI COMPONENT ---
|
| 14 |
+
print("Loading GenAI Component on CPU safely for ZeroGPU startup...")
|
| 15 |
|
| 16 |
+
# Load the model strictly on CPU at the top level to prevent ZeroGPU startup crashes.
|
| 17 |
gen_tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 18 |
gen_model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
|
| 19 |
|
| 20 |
def generate_sales_pitch(user_query, company_name, sector, theme, description):
|
|
|
|
| 21 |
prompt = (
|
| 22 |
f"Based on this company description: '{description[:300]}'\n\n"
|
| 23 |
f"Write one single, highly persuasive sentence explaining why investing in {company_name} "
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
try:
|
| 28 |
+
# We securely move the model to the GPU only *inside* the function after startup!
|
| 29 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 30 |
gen_model.to(device)
|
| 31 |
inputs = gen_tokenizer(prompt, return_tensors="pt", max_length=256, truncation=True).to(device)
|
|
|
|
| 56 |
df['full_text'] = df['sector'] + " - " + df['theme'] + ": " + df['description']
|
| 57 |
|
| 58 |
if 'embedding_model' not in globals():
|
| 59 |
+
print("Auto-loading embedding model 'paraphrase-MiniLM-L3-v2' strictly on CPU...")
|
| 60 |
+
# THE FIX: Explicitly lock SentenceTransformer to CPU so it doesn't crash the ZeroGPU bootloader!
|
| 61 |
+
embedding_model = SentenceTransformer('paraphrase-MiniLM-L3-v2', device='cpu')
|
| 62 |
|
| 63 |
if 'faiss_index' not in globals():
|
| 64 |
try:
|
|
|
|
| 67 |
faiss_index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 68 |
faiss_index.add(np.array(embeddings).astype('float32'))
|
| 69 |
except FileNotFoundError:
|
| 70 |
+
print("Saved embeddings not found! Generating them now (this may take 30 seconds)...")
|
| 71 |
embeddings = embedding_model.encode(df['full_text'].tolist(), show_progress_bar=False)
|
| 72 |
faiss_index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 73 |
faiss_index.add(np.array(embeddings).astype('float32'))
|
|
|
|
| 145 |
|
| 146 |
tier_label = match_labels[rank - 1] if rank <= len(match_labels) else "Match"
|
| 147 |
|
|
|
|
| 148 |
output_markdown += f"#### #{rank}. {c_name} (`{ticker}`) — *{sector}*\n\n"
|
| 149 |
output_markdown += f"**{tier_label}** (Similarity Score: `{score*100:.1f}%`)\n\n"
|
| 150 |
output_markdown += f"**Industry:** {theme}\n\n"
|
| 151 |
output_markdown += f"**GenAI Investment Pitch:** *'{pitch}'*\n\n"
|
| 152 |
output_markdown += f"**Company Overview:** {desc[:200]}...\n\n---\n\n"
|
| 153 |
|
|
|
|
| 154 |
yield output_markdown
|
| 155 |
|
| 156 |
|