Spaces:
Runtime error
Runtime error
demo interface
Browse files
app.py
CHANGED
@@ -1,30 +1,80 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
# Assuming `my_gpt_model` is your custom model's function that takes a date range and a ticker and returns a string.
|
4 |
-
def predict_with_gpt(start_date, end_date, ticker):
|
5 |
-
# Your model would use the start_date, end_date, and ticker to generate a prediction.
|
6 |
-
# For this example, we'll just return a dummy string.
|
7 |
-
prediction = ", ".join([start_date, end_date, ticker])
|
8 |
-
return prediction
|
9 |
-
|
10 |
-
# Create the Gradio app
|
11 |
-
def create_gradio_app():
|
12 |
-
with gr.Blocks() as app:
|
13 |
-
gr.Markdown("Enter a range of dates and a stock ticker to get predictions from the GPT model.")
|
14 |
-
with gr.Row():
|
15 |
-
start_date = gr.Date(label="Start Date")
|
16 |
-
end_date = gr.Date(label="End Date")
|
17 |
-
ticker = gr.Textbox(label="Ticker")
|
18 |
-
output = gr.Textbox(label="GPT Model Output")
|
19 |
-
|
20 |
-
# When the button is clicked, the `predict_with_gpt` function is called
|
21 |
-
gr.Button("Predict").click(
|
22 |
-
predict_with_gpt,
|
23 |
-
inputs=[start_date, end_date, ticker],
|
24 |
-
outputs=output
|
25 |
-
)
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
app.launch()
|
|
|
1 |
+
import re
|
2 |
import gradio as gr
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
from peft import PeftModel
|
5 |
+
from datetime import date
|
6 |
+
|
7 |
+
|
8 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
9 |
+
'meta-llama/Llama-2-7b-chat-hf',
|
10 |
+
trust_remote_code=True,
|
11 |
+
device_map="auto",
|
12 |
+
)
|
13 |
+
model = PeftModel.from_pretrained(
|
14 |
+
base_model,
|
15 |
+
'FinGPT/fingpt-forecaster_dow30_llama2-7b_lora'
|
16 |
+
)
|
17 |
+
model = model.eval()
|
18 |
+
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-2-7b-chat-hf')
|
20 |
+
|
21 |
+
|
22 |
+
def construct_prompt(ticker, date, n_weeks):
|
23 |
+
|
24 |
+
return ", ".join([ticker, date, str(n_weeks)])
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
def get_curday():
|
28 |
+
|
29 |
+
return date.today().strftime("%Y-%m-%d")
|
30 |
+
|
31 |
+
|
32 |
+
def predict(ticker, date, n_weeks):
|
33 |
+
|
34 |
+
prompt = construct_prompt(ticker, date, n_weeks)
|
35 |
+
|
36 |
+
# inputs = tokenizer(
|
37 |
+
# prompt, return_tensors='pt',
|
38 |
+
# padding=False, max_length=4096
|
39 |
+
# )
|
40 |
+
# inputs = {key: value.to(model.device) for key, value in inputs.items()}
|
41 |
+
|
42 |
+
# res = model.generate(
|
43 |
+
# **inputs, max_length=4096, do_sample=True,
|
44 |
+
# eos_token_id=tokenizer.eos_token_id,
|
45 |
+
# use_cache=True
|
46 |
+
# )
|
47 |
+
# output = tokenizer.decode(res[0], skip_special_tokens=True)
|
48 |
+
# answer = re.sub(r'.*\[/INST\]\s*', '', output, flags=re.DOTALL)
|
49 |
+
|
50 |
+
answer = prompt
|
51 |
+
|
52 |
+
return answer
|
53 |
+
|
54 |
+
|
55 |
+
demo = gr.Interface(
|
56 |
+
predict,
|
57 |
+
inputs=[
|
58 |
+
gr.Textbox(
|
59 |
+
label="Ticker",
|
60 |
+
value="AAPL",
|
61 |
+
info="Companys from Dow-30 are recommended"
|
62 |
+
)
|
63 |
+
gr.Textbox(
|
64 |
+
label="Date",
|
65 |
+
value=get_curday,
|
66 |
+
info="Date from which the prediction is made, use format 'yyyy-mm-dd'"
|
67 |
+
),
|
68 |
+
gr.Slider(
|
69 |
+
minimum=1,
|
70 |
+
maximum=4,
|
71 |
+
value=3,
|
72 |
+
step=1,
|
73 |
+
label="n_weeks",
|
74 |
+
info="Information of the past n weeks will be utilized, choose between 1 and 4"
|
75 |
+
),
|
76 |
+
],
|
77 |
+
outputs="Response"
|
78 |
+
)
|
79 |
|
80 |
+
demo.launch()
|
|