File size: 7,690 Bytes
c8255b3
37d7157
c8255b3
 
 
 
 
 
 
9051e7c
 
 
 
c8255b3
 
 
 
9051e7c
 
 
 
 
 
 
 
 
 
 
33ef098
 
 
 
9051e7c
 
 
 
 
c8255b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be76452
c8255b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9051e7c
 
 
 
 
 
 
 
 
 
37d7157
 
9051e7c
c8255b3
 
 
 
 
 
 
 
 
 
 
 
 
9051e7c
c8255b3
 
 
 
 
9051e7c
 
c8255b3
9051e7c
 
 
c8255b3
 
9051e7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
  This is a Hugging Face Spaces demo for Fin-RWKV-1B5 attention free financial expert modal.
  Author: Umut (Hope) YILDIRIM <[email protected]>
"""

import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
from threading import Thread
import torch
# from openbb import obb

# Login to OpenBB
# obb.account.login()

tokenizer = AutoTokenizer.from_pretrained("umuthopeyildirim/fin-rwkv-1b5")
model = AutoModelForCausalLM.from_pretrained("umuthopeyildirim/fin-rwkv-1b5")

products = [
    {"name": "Red Bull", "description": "Energy Drink", "symbol": "ALI",
        "image": "https://i.pinimg.com/originals/f9/68/e3/f968e3a1d474e1c19a91b508231a88c8.png"},
    {"name": "iPhone 15", "description": "Latest Apple Smartphone", "symbol": "PL",
        "image": "https://www.apple.com/newsroom/images/2023/09/apple-unveils-iphone-15-pro-and-iphone-15-pro-max/tile/Apple-iPhone-15-Pro-lineup-hero-230912.jpg.news_app_ed.jpg"},
    {"name": "Pencil", "description": "Wooden Writing Tool", "symbol": "CL",
        "image": "https://m.media-amazon.com/images/I/41IQfWzTojL._AC_UF1000,1000_QL80_.jpg"},
    {"name": "LG Monitor", "description": "High-Resolution Computer Monitor", "symbol": "NG",
        "image": "https://media.us.lg.com/transform/ecomm-PDPGallery-1100x730/a330bb92-bc33-435a-aa1b-07795eedbc10/md08003490-DZ-03"},
]
examples_openbb = [
    "x,Aluminum Futures\n 2024-01-02T00:00:00,2278.75\n 2024-01-03T00:00:00,2251\n 2024-01-04T00:00:00,2218.25\n 2024-01-05T00:00:00,2203\n 2024-01-08T00:00:00,2168.5\n 2024-01-09T00:00:00,2182.5\n 2024-01-10T00:00:00,2178.5\n 2024-01-11T00:00:00,2179.75\n 2024-01-12T00:00:00,2167.5\n 2024-01-16T00:00:00,2161.75\n 2024-01-17T00:00:00,2148.75\n 2024-01-18T00:00:00,2136.5\n 2024-01-19T00:00:00,2127.5\n 2024-01-22T00:00:00,2127.5\n 2024-01-23T00:00:00,2184.25\n 2024-01-24T00:00:00,2186.25\n 2024-01-25T00:00:00,2194.5\n 2024-01-26T00:00:00,2246.25\n Here is the historical data for Aluminum Futures. Did it appreciated over time or depreciated?\n",
    "x,Platinum Futures\n 2024-01-02T00:00:00,985.5\n 2024-01-03T00:00:00,974.4000244140624\n 2024-01-04T00:00:00,953.9000244140624\n 2024-01-05T00:00:00,959.2999877929688\n 2024-01-08T00:00:00,946.7000122070312\n 2024-01-09T00:00:00,931\n 2024-01-10T00:00:00,918.7999877929688\n 2024-01-11T00:00:00,909\n 2024-01-12T00:00:00,910.0999755859376\n 2024-01-16T00:00:00,895.0999755859375\n 2024-01-17T00:00:00,882.7999877929688\n 2024-01-18T00:00:00,903.2999877929688\n 2024-01-19T00:00:00,897.2999877929688\n 2024-01-22T00:00:00,894.4000244140625\n 2024-01-23T00:00:00,896.2000122070312\n 2024-01-24T00:00:00,905.5999755859376\n 2024-01-25T00:00:00,887.4000244140625\n 2024-01-26T00:00:00,921.7000122070312\n Here is the historical data for Platinum Futures. Did it appreciated over time or depreciated?\n",
    "x,Natural Gas Futures\n 2024-01-02T00:00:00,2.568000078201294\n 2024-01-03T00:00:00,2.6679999828338623\n 2024-01-04T00:00:00,2.821000099182129\n 2024-01-05T00:00:00,2.8929998874664307\n 2024-01-08T00:00:00,2.9800000190734863\n 2024-01-09T00:00:00,3.190000057220459\n 2024-01-10T00:00:00,3.0390000343322754\n 2024-01-11T00:00:00,3.0969998836517334\n 2024-01-12T00:00:00,3.312999963760376\n 2024-01-15T00:00:00,3.1059999465942383\n 2024-01-16T00:00:00,2.9000000953674316\n 2024-01-17T00:00:00,2.869999885559082\n 2024-01-18T00:00:00,2.697000026702881\n 2024-01-19T00:00:00,2.5190000534057617\n 2024-01-22T00:00:00,2.4189999103546143\n 2024-01-23T00:00:00,2.450000047683716\n 2024-01-24T00:00:00,2.6410000324249268\n 2024-01-25T00:00:00,2.571000099182129\n 2024-01-26T00:00:00,2.7190001010894775 Here is the historical data for Natural Gas Futures. Did it appreciated over time or depreciated?\n",
    "x,WTI Crude Oil Futures\n 2024-01-02T00:00:00,70.37999725341797\n 2024-01-03T00:00:00,72.69999694824219\n 2024-01-04T00:00:00,72.19000244140625\n 2024-01-05T00:00:00,73.80999755859375\n 2024-01-08T00:00:00,70.7699966430664\n 2024-01-09T00:00:00,72.23999786376953\n 2024-01-10T00:00:00,71.37000274658203\n 2024-01-11T00:00:00,72.0199966430664\n 2024-01-12T00:00:00,72.68000030517578\n 2024-01-15T00:00:00,72.5\n 2024-01-16T00:00:00,72.4000015258789\n 2024-01-17T00:00:00,72.55999755859375\n 2024-01-18T00:00:00,74.08000183105469\n 2024-01-19T00:00:00,73.41000366210938\n 2024-01-22T00:00:00,75.19000244140625\n 2024-01-23T00:00:00,74.37000274658203\n 2024-01-24T00:00:00,75.08999633789062\n 2024-01-25T00:00:00,77.36000061035156\n 2024-01-26T00:00:00,78.2300033569336 Here is the historical data for WTI Crude Oil Futures. Did it appreciated over time or depreciated?\n",
]

openbb_inputbox = ""
openbb_chatbox = ""


class StopOnTokens(StoppingCriteria):
    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
        stop_ids = [29, 0]
        for stop_id in stop_ids:
            if input_ids[0][-1] == stop_id:
                return True
        return False


def predict(message, history):

    history_transformer_format = history + [[message, ""]]
    stop = StopOnTokens()

    messages = "".join(["".join(["\nuser :"+item[0], "\nbot:"+item[1]])  # curr_system_message +
                        for item in history_transformer_format])

    print(messages)

    model_inputs = tokenizer([messages], return_tensors="pt")
    streamer = TextIteratorStreamer(
        tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
    generate_kwargs = dict(
        model_inputs,
        streamer=streamer,
        max_new_tokens=1024,
        do_sample=True,
        top_p=0.95,
        top_k=1000,
        temperature=0.5,
        num_beams=1,
        stopping_criteria=StoppingCriteriaList([stop])
    )
    t = Thread(target=model.generate, kwargs=generate_kwargs)
    t.start()

    partial_message = ""
    for new_token in streamer:
        if new_token != '<':
            partial_message += new_token
            yield partial_message


def generate_text(prompt, tokenizer, model):
    # Tokenize the input
    input_ids = tokenizer.encode(prompt, return_tensors="pt")

    # Generate a response
    output = model.generate(input_ids, max_length=333, num_return_sequences=1)

    # Decode the output
    generated_text = tokenizer.decode(output[0], skip_special_tokens=True)

    return generated_text


# def fetch_historical_data(symbol):
#     # Replace with actual OpenBB method to fetch historical data
#     data = obb.derivatives.futures.historical(
#         symbol,
#         start_date="2024-01-01",
#         interval="1wk",
#     ).to_df()["close"].rename(symbol)
#     return data


title = "# Fin-RWKV: Attention Free Financial Expert (WIP)"
description = """Demo for **Fin-RWKV: Attention Free Financial Expert (WIP)**.
To download the model, please visit [Fin-RWKV: Attention Free Financial Expert (WIP)](https://huggingface.co/umuthopeyildirim/fin-rwkv-1b5). [Google Presentation](https://docs.google.com/presentation/d/1vNQ8Y5wwR0WXlO60fsXjkru5R9I0ZgykTmgag0B3Ato/edit?usp=sharing)"""

css = """
#img-display-container {
    max-height: 100vh;
    }
#img-display-input {
    max-height: 80vh;
    }
#img-display-output {
    max-height: 80vh;
    }
"""


with gr.Blocks(css=css) as demo:
    gr.Markdown(title)
    gr.Markdown(description)
    with gr.Tab("Chatbot"):
        gr.ChatInterface(predict)
    # with gr.Tab("E-Commerce"):
    #     e_commerce_interface()
    with gr.Tab("OpenBB"):
        gr.ChatInterface(predict, examples=[
                         examples_openbb[0], examples_openbb[1], examples_openbb[2], examples_openbb[3]])


if __name__ == '__main__':
    demo.queue().launch(share=False)