Spaces:
Sleeping
Sleeping
QAway-to
commited on
Commit
·
aa32941
1
Parent(s):
acb2292
Test1
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests, pandas as pd
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from datetime import datetime, timedelta
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# ============================================================
|
| 7 |
+
# 1️⃣ FRED API — экономические данные США
|
| 8 |
+
# ============================================================
|
| 9 |
+
API_KEY = "YOUR_FRED_API_KEY" # зарегистрируй на https://fred.stlouisfed.org/
|
| 10 |
+
FRED_URL = "https://api.stlouisfed.org/fred/series/observations"
|
| 11 |
+
|
| 12 |
+
INDICATORS = {
|
| 13 |
+
"GDP": "GDP",
|
| 14 |
+
"Inflation (CPI)": "CPIAUCSL",
|
| 15 |
+
"Unemployment Rate": "UNRATE",
|
| 16 |
+
"Interest Rate (Fed Funds)": "FEDFUNDS"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
def fetch_fred_data(series_id, start="2024-01-01", end=None):
|
| 20 |
+
if end is None:
|
| 21 |
+
end = datetime.now().strftime("%Y-%m-%d")
|
| 22 |
+
params = {
|
| 23 |
+
"series_id": series_id,
|
| 24 |
+
"api_key": API_KEY,
|
| 25 |
+
"file_type": "json",
|
| 26 |
+
"observation_start": start,
|
| 27 |
+
"observation_end": end
|
| 28 |
+
}
|
| 29 |
+
r = requests.get(FRED_URL, params=params)
|
| 30 |
+
data = r.json().get("observations", [])
|
| 31 |
+
df = pd.DataFrame(data)
|
| 32 |
+
if not df.empty:
|
| 33 |
+
df["value"] = pd.to_numeric(df["value"], errors="coerce")
|
| 34 |
+
return df
|
| 35 |
+
|
| 36 |
+
# ============================================================
|
| 37 |
+
# 2️⃣ Генерация сводки (LLM)
|
| 38 |
+
# ============================================================
|
| 39 |
+
generator = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 40 |
+
|
| 41 |
+
def generate_summary(topic):
|
| 42 |
+
sid = INDICATORS.get(topic)
|
| 43 |
+
df = fetch_fred_data(sid)
|
| 44 |
+
if df.empty:
|
| 45 |
+
return "⚠️ No data available for this indicator."
|
| 46 |
+
last = df.tail(5)
|
| 47 |
+
trend = last["value"].pct_change().mean() * 100
|
| 48 |
+
context = f"Recent values of {topic}:\n{last[['date','value']].to_string(index=False)}\n\nAverage change: {trend:.2f}%"
|
| 49 |
+
prompt = f"Provide an analytical summary of the trend:\n{context}"
|
| 50 |
+
summary = generator(prompt, max_new_tokens=150)[0]["generated_text"]
|
| 51 |
+
return summary, df
|
| 52 |
+
|
| 53 |
+
# ============================================================
|
| 54 |
+
# 3️⃣ Экспорт CSV → Power BI
|
| 55 |
+
# ============================================================
|
| 56 |
+
def export_csv(df, topic):
|
| 57 |
+
filename = f"powerbi_{topic.replace(' ', '_').lower()}.csv"
|
| 58 |
+
df.to_csv(filename, index=False)
|
| 59 |
+
return filename
|
| 60 |
+
|
| 61 |
+
# ============================================================
|
| 62 |
+
# 4️⃣ Gradio UI
|
| 63 |
+
# ============================================================
|
| 64 |
+
with gr.Blocks(title="🏦 RAG Financial Analytics → Power BI") as app:
|
| 65 |
+
gr.Markdown("## 🏦 Financial RAG: FRED API → LLM → Power BI")
|
| 66 |
+
|
| 67 |
+
topic = gr.Dropdown(list(INDICATORS.keys()), label="Select Indicator", value="Inflation (CPI)")
|
| 68 |
+
out_summary = gr.Textbox(label="Generated Summary", lines=8)
|
| 69 |
+
out_file = gr.File(label="Power BI Export")
|
| 70 |
+
|
| 71 |
+
def run_pipeline(topic):
|
| 72 |
+
summary, df = generate_summary(topic)
|
| 73 |
+
file_path = export_csv(df, topic)
|
| 74 |
+
return summary, file_path
|
| 75 |
+
|
| 76 |
+
gr.Button("Generate & Export").click(run_pipeline, inputs=topic, outputs=[out_summary, out_file])
|
| 77 |
+
|
| 78 |
+
app.launch()
|
fghgf.py
DELETED
|
File without changes
|