Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,125 +1,106 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
import torch
|
3 |
import requests
|
4 |
import os
|
5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
from huggingface_hub import login
|
7 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
8 |
-
from peft import PeftModel
|
9 |
-
import torch
|
10 |
-
|
11 |
-
@st.cache_resource
|
12 |
-
def load_fingpt_lora():
|
13 |
-
base_model_id = "meta-llama/Llama-2-7b-hf"
|
14 |
-
lora_adapter_id = "FinGPT/fingpt-mt_llama2-7b_lora"
|
15 |
-
tokenizer = AutoTokenizer.from_pretrained(base_model_id, use_auth_token=HF_TOKEN)
|
16 |
-
|
17 |
-
base_model = AutoModelForCausalLM.from_pretrained(
|
18 |
-
base_model_id,
|
19 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
20 |
-
device_map="auto",
|
21 |
-
use_auth_token=HF_TOKEN
|
22 |
-
)
|
23 |
-
|
24 |
-
model = PeftModel.from_pretrained(base_model, lora_adapter_id, use_auth_token=HF_TOKEN)
|
25 |
-
return model, tokenizer
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
HF_TOKEN = os.getenv("Allie", None)
|
30 |
if HF_TOKEN:
|
31 |
login(HF_TOKEN)
|
32 |
|
33 |
-
#
|
34 |
model_map = {
|
35 |
-
"
|
36 |
-
"
|
37 |
-
"
|
38 |
-
"
|
39 |
-
"
|
40 |
}
|
41 |
|
42 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
@st.cache_resource
|
44 |
def load_local_model(model_id):
|
45 |
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=HF_TOKEN)
|
46 |
model = AutoModelForCausalLM.from_pretrained(
|
47 |
model_id,
|
48 |
torch_dtype=torch.float32,
|
49 |
-
device_map="auto"
|
50 |
use_auth_token=HF_TOKEN
|
51 |
)
|
52 |
return model, tokenizer
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
#
|
64 |
-
def clean_output(output_text):
|
65 |
-
parts = output_text.split("FinGPT:")
|
66 |
-
return parts[-1].strip() if len(parts) > 1 else output_text.strip()
|
67 |
-
|
68 |
-
# === Generate with local model ===
|
69 |
def query_local_model(model_id, prompt):
|
70 |
model, tokenizer = load_local_model(model_id)
|
71 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
72 |
outputs = model.generate(
|
73 |
**inputs,
|
74 |
-
max_new_tokens=
|
75 |
temperature=0.7,
|
76 |
-
|
77 |
-
|
78 |
repetition_penalty=1.2,
|
79 |
do_sample=True,
|
80 |
pad_token_id=tokenizer.eos_token_id,
|
81 |
eos_token_id=tokenizer.eos_token_id
|
82 |
)
|
83 |
-
|
84 |
-
return clean_output(raw_output)
|
85 |
|
86 |
-
#
|
87 |
def query_remote_model(model_id, prompt):
|
88 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
89 |
-
payload = {"inputs": prompt, "parameters": {"max_new_tokens":
|
90 |
response = requests.post(
|
91 |
f"https://api-inference.huggingface.co/models/{model_id}",
|
92 |
headers=headers,
|
93 |
json=payload
|
94 |
)
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
if model_entry["local"]:
|
105 |
return query_local_model(model_entry["id"], prompt)
|
106 |
else:
|
107 |
-
return
|
108 |
|
109 |
-
# ===
|
110 |
-
st.set_page_config(page_title="
|
111 |
-
st.title("
|
112 |
|
113 |
-
|
114 |
-
|
|
|
115 |
|
116 |
-
if st.button("
|
117 |
-
with st.spinner("
|
|
|
118 |
try:
|
119 |
-
|
120 |
-
answer = query_model(model_entry, user_question)
|
121 |
-
st.text_area("💬 Response:", value=answer, height=300, disabled=True)
|
122 |
except Exception as e:
|
123 |
-
|
|
|
|
|
124 |
|
125 |
|
|
|
1 |
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
import torch
|
4 |
import requests
|
5 |
import os
|
6 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
7 |
from huggingface_hub import login
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
HF_TOKEN = os.getenv("Allie") or "<your_token_here>"
|
|
|
10 |
if HF_TOKEN:
|
11 |
login(HF_TOKEN)
|
12 |
|
13 |
+
# Define model map
|
14 |
model_map = {
|
15 |
+
"InvestLM": {"id": "yixuantt/InvestLM-mistral-AWQ", "local": False},
|
16 |
+
"FinLLaMA": {"id": "us4/fin-llama3.1-8b", "local": False},
|
17 |
+
"FinanceConnect": {"id": "ceadar-ie/FinanceConnect-13B", "local": True},
|
18 |
+
"Sujet-Finance": {"id": "sujet-ai/Sujet-Finance-8B-v0.1", "local": True},
|
19 |
+
"FinGPT (LoRA)": {"id": "FinGPT/fingpt-mt_llama2-7b_lora", "local": True} # Placeholder, special handling below
|
20 |
}
|
21 |
|
22 |
+
# Load question list
|
23 |
+
@st.cache_data
|
24 |
+
def load_questions():
|
25 |
+
df = pd.read_csv("questions.csv")
|
26 |
+
return df["Question"].dropna().tolist()
|
27 |
+
|
28 |
+
# Load local models
|
29 |
@st.cache_resource
|
30 |
def load_local_model(model_id):
|
31 |
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=HF_TOKEN)
|
32 |
model = AutoModelForCausalLM.from_pretrained(
|
33 |
model_id,
|
34 |
torch_dtype=torch.float32,
|
35 |
+
device_map="auto",
|
36 |
use_auth_token=HF_TOKEN
|
37 |
)
|
38 |
return model, tokenizer
|
39 |
|
40 |
+
# Prompt template
|
41 |
+
PROMPT_TEMPLATE = (
|
42 |
+
"You are FinGPT, a highly knowledgeable and reliable financial assistant.\n"
|
43 |
+
"Explain the following finance/tax/controlling question clearly, including formulas, examples, and reasons why it matters.\n"
|
44 |
+
"\n"
|
45 |
+
"Question: {question}\n"
|
46 |
+
"Answer:"
|
47 |
+
)
|
48 |
|
49 |
+
# Local generation
|
|
|
|
|
|
|
|
|
|
|
50 |
def query_local_model(model_id, prompt):
|
51 |
model, tokenizer = load_local_model(model_id)
|
52 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
53 |
outputs = model.generate(
|
54 |
**inputs,
|
55 |
+
max_new_tokens=400,
|
56 |
temperature=0.7,
|
57 |
+
top_p=0.9,
|
58 |
+
top_k=40,
|
59 |
repetition_penalty=1.2,
|
60 |
do_sample=True,
|
61 |
pad_token_id=tokenizer.eos_token_id,
|
62 |
eos_token_id=tokenizer.eos_token_id
|
63 |
)
|
64 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
65 |
|
66 |
+
# Remote HF inference
|
67 |
def query_remote_model(model_id, prompt):
|
68 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
69 |
+
payload = {"inputs": prompt, "parameters": {"max_new_tokens": 400}}
|
70 |
response = requests.post(
|
71 |
f"https://api-inference.huggingface.co/models/{model_id}",
|
72 |
headers=headers,
|
73 |
json=payload
|
74 |
)
|
75 |
+
result = response.json()
|
76 |
+
return result[0]["generated_text"] if isinstance(result, list) else result.get("generated_text", "ERROR")
|
77 |
+
|
78 |
+
# Route to appropriate model
|
79 |
+
def query_model(model_entry, question):
|
80 |
+
prompt = PROMPT_TEMPLATE.format(question=question)
|
81 |
+
if model_entry["id"] == "FinGPT/fingpt-mt_llama2-7b_lora":
|
82 |
+
return "⚠️ FinGPT (LoRA) integration requires manual loading with PEFT and is not available via HF API."
|
83 |
+
elif model_entry["local"]:
|
|
|
84 |
return query_local_model(model_entry["id"], prompt)
|
85 |
else:
|
86 |
+
return query_remote_model(model_entry["id"], prompt)
|
87 |
|
88 |
+
# === UI ===
|
89 |
+
st.set_page_config(page_title="Finanzmodell Tester", layout="centered")
|
90 |
+
st.title("📊 Finanzmodell Vergleichs-Interface")
|
91 |
|
92 |
+
questions = load_questions()
|
93 |
+
question_choice = st.selectbox("Wähle eine Frage", questions)
|
94 |
+
model_choice = st.selectbox("Wähle ein Modell", list(model_map.keys()))
|
95 |
|
96 |
+
if st.button("Antwort generieren"):
|
97 |
+
with st.spinner("Antwort wird generiert..."):
|
98 |
+
model_entry = model_map[model_choice]
|
99 |
try:
|
100 |
+
answer = query_model(model_entry, question_choice)
|
|
|
|
|
101 |
except Exception as e:
|
102 |
+
answer = f"[Fehler: {str(e)}]"
|
103 |
+
st.text_area("💬 Antwort des Modells:", value=answer, height=400, disabled=True)
|
104 |
+
|
105 |
|
106 |
|