Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import faiss
|
| 3 |
import gradio as gr
|
| 4 |
from datasets import load_dataset
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from groq import Groq
|
| 7 |
-
from google.colab import userdata
|
| 8 |
|
| 9 |
# ================================
|
| 10 |
# 1. Initialize Groq Client
|
| 11 |
# ================================
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# ================================
|
| 15 |
# 2. Load Datasets (SCRIPT-FREE)
|
|
@@ -17,36 +23,26 @@ client = Groq(api_key=userdata.get("Finance_API"))
|
|
| 17 |
print("Loading datasets...")
|
| 18 |
|
| 19 |
# Financial News dataset
|
| 20 |
-
news_ds = load_dataset("ashraq/financial-news", split="train[:
|
| 21 |
-
|
| 22 |
-
# NOTE: Removing problematic stock market dataset loading for now.
|
| 23 |
-
# stock_ds = load_dataset("ashraq/stock_price_data", split="train[:1000]")
|
| 24 |
|
| 25 |
# Create a small QA dataset from Financial News (simple heuristic)
|
| 26 |
qa_docs = []
|
| 27 |
for item in news_ds:
|
| 28 |
-
# Changed 'text' to 'headline'
|
| 29 |
headline = item["headline"]
|
| 30 |
-
# Split into sentences and make simple Q/A
|
| 31 |
sentences = headline.split(". ")
|
| 32 |
-
for s in sentences[:2]:
|
| 33 |
-
if len(s) > 30:
|
| 34 |
qa_docs.append(f"Question: What does the news say?\nAnswer: {s}")
|
| 35 |
|
| 36 |
documents = []
|
| 37 |
|
| 38 |
# Add Financial News
|
| 39 |
for item in news_ds:
|
| 40 |
-
# Changed 'text' to 'headline'
|
| 41 |
documents.append(item["headline"])
|
| 42 |
|
| 43 |
-
# Add
|
| 44 |
documents.extend(qa_docs)
|
| 45 |
|
| 46 |
-
# NOTE: Removed stock dataset from document aggregation as well.
|
| 47 |
-
# for item in stock_ds:
|
| 48 |
-
# documents.append(str(item))
|
| 49 |
-
|
| 50 |
print(f"Total documents loaded: {len(documents)}")
|
| 51 |
|
| 52 |
# ================================
|
|
@@ -129,7 +125,7 @@ interface = gr.Interface(
|
|
| 129 |
inputs=gr.Textbox(lines=3, placeholder="Example: I have $5000, where should I invest?"),
|
| 130 |
outputs="text",
|
| 131 |
title="π RAG-Based Financial Investment Advisor",
|
| 132 |
-
description="Uses Financial News +
|
| 133 |
)
|
| 134 |
|
| 135 |
-
interface.launch(
|
|
|
|
| 1 |
+
import os
|
| 2 |
import numpy as np
|
| 3 |
import faiss
|
| 4 |
import gradio as gr
|
| 5 |
from datasets import load_dataset
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
from groq import Groq
|
|
|
|
| 8 |
|
| 9 |
# ================================
|
| 10 |
# 1. Initialize Groq Client
|
| 11 |
# ================================
|
| 12 |
+
# Groq API Key must be set in Hugging Face Space Secrets
|
| 13 |
+
# Go to Settings -> Secrets and add Finance_API
|
| 14 |
+
GROQ_API_KEY = os.environ.get("Finance_API")
|
| 15 |
+
if not GROQ_API_KEY:
|
| 16 |
+
raise ValueError("Please set the Finance_API secret in your Hugging Face Space.")
|
| 17 |
+
|
| 18 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 19 |
|
| 20 |
# ================================
|
| 21 |
# 2. Load Datasets (SCRIPT-FREE)
|
|
|
|
| 23 |
print("Loading datasets...")
|
| 24 |
|
| 25 |
# Financial News dataset
|
| 26 |
+
news_ds = load_dataset("ashraq/financial-news", split="train[:500]") # reduce size for runtime
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Create a small QA dataset from Financial News (simple heuristic)
|
| 29 |
qa_docs = []
|
| 30 |
for item in news_ds:
|
|
|
|
| 31 |
headline = item["headline"]
|
|
|
|
| 32 |
sentences = headline.split(". ")
|
| 33 |
+
for s in sentences[:2]:
|
| 34 |
+
if len(s) > 30:
|
| 35 |
qa_docs.append(f"Question: What does the news say?\nAnswer: {s}")
|
| 36 |
|
| 37 |
documents = []
|
| 38 |
|
| 39 |
# Add Financial News
|
| 40 |
for item in news_ds:
|
|
|
|
| 41 |
documents.append(item["headline"])
|
| 42 |
|
| 43 |
+
# Add QA docs
|
| 44 |
documents.extend(qa_docs)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
print(f"Total documents loaded: {len(documents)}")
|
| 47 |
|
| 48 |
# ================================
|
|
|
|
| 125 |
inputs=gr.Textbox(lines=3, placeholder="Example: I have $5000, where should I invest?"),
|
| 126 |
outputs="text",
|
| 127 |
title="π RAG-Based Financial Investment Advisor",
|
| 128 |
+
description="Uses Financial News + FAISS + Groq LLaMA (Script-Free datasets)"
|
| 129 |
)
|
| 130 |
|
| 131 |
+
interface.launch()
|