4lli39421 commited on
Commit
2d835c9
·
verified ·
1 Parent(s): 82857cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -74
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
- # Load token from Hugging Face Space secrets
29
- HF_TOKEN = os.getenv("Allie", None)
30
  if HF_TOKEN:
31
  login(HF_TOKEN)
32
 
33
- # === Available Models for Selection ===
34
  model_map = {
35
- "FinGPT LoRA" : {"id": "FinGPT/fingpt-mt_llama2-7b_lora", "local": True, "custom_loader": load_fingpt_lora},
36
- "InvestLM (AWQ)": {"id": "yixuantt/InvestLM-mistral-AWQ", "local": False},
37
- "FinLLaMA (LLaMA3.1-8B)": {"id": "us4/fin-llama3.1-8b", "local": False},
38
- "FinanceConnect (13B)": {"id": "ceadar-ie/FinanceConnect-13B", "local": True},
39
- "Sujet-Finance (8B)": {"id": "sujet-ai/Sujet-Finance-8B-v0.1", "local": True}
40
  }
41
 
42
- # === Load local models with caching ===
 
 
 
 
 
 
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" if torch.cuda.is_available() else None,
50
  use_auth_token=HF_TOKEN
51
  )
52
  return model, tokenizer
53
 
54
- # === Build system prompt for discursive answers ===
55
- def build_prompt(user_question):
56
- return (
57
- "You are FinGPT, a helpful and knowledgeable financial assistant. "
58
- "You explain finance, controlling, and tax topics clearly, with examples when useful.\n\n"
59
- f"User: {user_question.strip()}\n"
60
- "FinGPT:"
61
- )
62
 
63
- # === Clean repeated/extra outputs ===
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=300,
75
  temperature=0.7,
76
- top_k=50,
77
- top_p=0.95,
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
- raw_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
84
- return clean_output(raw_output)
85
 
86
- # === Generate with remote HF API ===
87
  def query_remote_model(model_id, prompt):
88
- headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
89
- payload = {"inputs": prompt, "parameters": {"max_new_tokens": 300}}
90
  response = requests.post(
91
  f"https://api-inference.huggingface.co/models/{model_id}",
92
  headers=headers,
93
  json=payload
94
  )
95
- if response.status_code == 200:
96
- result = response.json()
97
- return result[0]["generated_text"] if isinstance(result, list) else result.get("generated_text", "No output")
98
- else:
99
- raise RuntimeError(f"API Error {response.status_code}: {response.text}")
100
-
101
- # === Unified model query handler ===
102
- def query_model(model_entry, user_question):
103
- prompt = build_prompt(user_question)
104
- if model_entry["local"]:
105
  return query_local_model(model_entry["id"], prompt)
106
  else:
107
- return clean_output(query_remote_model(model_entry["id"], prompt))
108
 
109
- # === Streamlit UI Layout ===
110
- st.set_page_config(page_title="Finance LLM Comparison", layout="centered")
111
- st.title("💼 Financial LLM Evaluation Interface")
112
 
113
- model_choice = st.selectbox("Select a Financial Model", list(model_map.keys()))
114
- user_question = st.text_area("Enter your financial question:", "What is EBIT vs EBITDA?", height=150)
 
115
 
116
- if st.button("Get Response"):
117
- with st.spinner("Thinking like a CFO..."):
 
118
  try:
119
- model_entry = model_map[model_choice]
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
- st.error(f"❌ Error: {e}")
 
 
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