MuhammadHamza33 commited on
Commit
69c5112
·
1 Parent(s): 860452d
Files changed (1) hide show
  1. app.py +19 -78
app.py CHANGED
@@ -1,97 +1,38 @@
1
  import gradio as gr
2
  import torch
3
- import os
4
-
5
  from transformers import AutoTokenizer, AutoModelForCausalLM
6
- from huggingface_hub import login
7
-
8
-
9
- # ------------------------------
10
- # Authenticate Hugging Face token
11
- # ------------------------------
12
- login(token=os.getenv("HF_TOKEN"))
13
 
14
- MODEL_ID = "meta-llama/Llama-3.2-1B-Instruct"
15
- token = os.getenv("HF_TOKEN")
16
-
17
-
18
- # ------------------------------
19
- # Load tokenizer + model
20
- # ------------------------------
21
- tokenizer = AutoTokenizer.from_pretrained(
22
- MODEL_ID,
23
- use_auth_token=token
24
- )
25
 
 
26
  model = AutoModelForCausalLM.from_pretrained(
27
  MODEL_ID,
28
- torch_dtype=torch.float32, # safer on CPU
29
- low_cpu_mem_usage=True,
30
- device_map="cpu",
31
- use_auth_token=token
32
  )
33
 
34
-
35
- # ------------------------------
36
- # System Prompt for FINBOT
37
- # ------------------------------
38
  SYSTEM_PROMPT = """
39
  You are FINBOT, a precise financial assistant.
40
-
41
- RULES:
42
- - NEVER repeat the user's question.
43
- - NEVER generate extra questions.
44
- - NEVER explain unless asked.
45
- - If data is inside <CONTEXT> or <DB_RESULTS>, use ONLY that data.
46
- - If calculation is required, output ONLY the final number unless the user says “explain”.
47
- - If asked for analysis, give short bullet points.
48
- - If data is missing, reply: “Data not available”.
49
- - Be concise and accurate.
50
  """
51
 
52
-
53
- # ------------------------------
54
- # Build proper Llama-3 prompt
55
- # ------------------------------
56
  def build_prompt(user_msg):
57
- return f"""
58
- <s>[INST] <<SYS>>
59
- {SYSTEM_PROMPT}
60
- <</SYS>>
61
-
62
- {user_msg}
63
- [/INST]
64
- """
65
 
66
-
67
- # ------------------------------
68
- # Generate function
69
- # ------------------------------
70
  def generate(user_input):
71
  prompt = build_prompt(user_input)
72
 
73
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
74
-
75
- with torch.no_grad():
76
- outputs = model.generate(
77
- **inputs,
78
- max_new_tokens=100,
79
- temperature=0.0, # deterministic (no randomness)
80
- do_sample=False,
81
- )
82
-
83
- result = tokenizer.decode(outputs[0], skip_special_tokens=True)
84
- return result.strip()
85
-
86
-
87
- # ------------------------------
88
- # Gradio UI
89
- # ------------------------------
90
- demo = gr.Interface(
91
- fn=generate,
92
- inputs=gr.Textbox(lines=5, label="Ask FINBOT"),
93
- outputs=gr.Textbox(label="FINBOT Answer"),
94
- title="FINBOT - Financial Assistant (Llama 3.2 1B)"
95
- )
96
 
97
- demo.launch()
 
1
  import gradio as gr
2
  import torch
 
 
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
 
 
 
 
4
 
5
+ MODEL_ID = "WiroAI/WiroAI-Finance-Qwen-1.5B"
 
 
 
 
 
 
 
 
 
 
6
 
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
8
  model = AutoModelForCausalLM.from_pretrained(
9
  MODEL_ID,
10
+ torch_dtype="auto",
11
+ device_map="cpu"
 
 
12
  )
13
 
 
 
 
 
14
  SYSTEM_PROMPT = """
15
  You are FINBOT, a precise financial assistant.
16
+ Follow the rules strictly:
17
+ - Never repeat the question
18
+ - Never explain unless asked
19
+ - Use <CONTEXT> or <DB_RESULTS> only if provided
20
+ - Output only the final number for calculations
 
 
 
 
 
21
  """
22
 
 
 
 
 
23
  def build_prompt(user_msg):
24
+ return f"{SYSTEM_PROMPT}\n\nUser: {user_msg}\nAssistant:"
 
 
 
 
 
 
 
25
 
 
 
 
 
26
  def generate(user_input):
27
  prompt = build_prompt(user_input)
28
 
29
+ inputs = tokenizer(prompt, return_tensors="pt")
30
+ outputs = model.generate(
31
+ **inputs,
32
+ max_new_tokens=80,
33
+ temperature=0.0,
34
+ do_sample=False
35
+ )
36
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ gr.Interface(fn=generate, inputs="text", outputs="text").launch()