aumkar commited on
Commit
8767050
1 Parent(s): d7687fa

app.py created

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # Load the model and tokenizer
6
+ @st.cache(allow_output_mutation=True)
7
+ def load_model():
8
+ model = AutoModelForCausalLM.from_pretrained("AdaptLLM/finance-chat")
9
+ tokenizer = AutoTokenizer.from_pretrained("AdaptLLM/finance-chat", use_fast=False)
10
+ return model, tokenizer
11
+
12
+ model, tokenizer = load_model()
13
+
14
+ # Streamlit interface
15
+ st.title("Finance Chatbot")
16
+
17
+ # User input
18
+ user_input = st.text_area("Enter your query:")
19
+
20
+ if st.button("Submit"):
21
+ if user_input:
22
+ # Prepare the prompt
23
+ prompt = f"<s>[INST] <<SYS>>\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\n<</SYS>>\n\n{user_input} [/INST]"
24
+
25
+ # Tokenize and generate response
26
+ inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).input_ids.to(model.device)
27
+ outputs = model.generate(input_ids=inputs, max_length=4096)[0]
28
+
29
+ answer_start = int(inputs.shape[-1])
30
+ pred = tokenizer.decode(outputs[answer_start:], skip_special_tokens=True)
31
+
32
+ # Display the output
33
+ st.write("### Assistant Output:")
34
+ st.write(pred)
35
+ else:
36
+ st.write("Please enter a query.")