File size: 3,463 Bytes
b63ab56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa4c05b
b63ab56
 
aa4c05b
b63ab56
 
 
 
aa4c05b
b63ab56
58a98b6
aa4c05b
b63ab56
 
 
 
 
 
 
 
aa4c05b
b63ab56
aa4c05b
 
3238e2c
 
 
 
 
 
 
 
b63ab56
3238e2c
b63ab56
 
 
 
 
 
 
 
 
 
 
 
 
 
3238e2c
 
b63ab56
 
 
3238e2c
 
34812cc
 
b63ab56
 
 
 
34812cc
 
 
 
b63ab56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34812cc
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
# import gradio as gr
# from transformers import AutoTokenizer, AutoModelForCausalLM
# import torch

# model = AutoModelForCausalLM.from_pretrained(
#     "Cogwisechat/falcon-7b-finance",
#     torch_dtype=torch.bfloat16,
#     trust_remote_code=True,
#     device_map="auto",
#     low_cpu_mem_usage=True,
# )
# tokenizer = AutoTokenizer.from_pretrained("Cogwisechat/falcon-7b-finance")


# def generate_text(input_text):
#     global output_text

#     input_ids = tokenizer.encode(input_text, return_tensors="pt")
#     attention_mask = torch.ones(input_ids.shape)

#     output = model.generate(
#         input_ids,
#         attention_mask=attention_mask,
#         max_length=200,
#         do_sample=True,
#         top_k=10,
#         num_return_sequences=1,
#         eos_token_id=tokenizer.eos_token_id,
#     )
    

#     output_text = tokenizer.decode(output[0], skip_special_tokens=True)
#     print(output_text)

#     # Remove Prompt Echo from Generated Text
    
#     cleaned_output_text = output_text.replace(input_text, "")
#     return  cleaned_output_text

# block = gr.Blocks()


# with block:
#     gr.Markdown("""<h1><center>CogwiseAI falcon7b</center></h1>
#     """)
#     # chatbot = gr.Chatbot()
#     message = gr.Textbox(placeholder='Enter Your Question Here')
#     state = gr.State()
#     submit = gr.Button("SEND")
#     submit.click(generate_text, inputs=[message, state], outputs=[output_text, state])

# block.launch(debug = True)










    

# # logo = (
# #             "<div >"
# #             "<img  src='ai-icon.png'alt='image One'>"
# #             + "</div>"
# #     )
# # text_generation_interface = gr.Interface(
# #     fn=generate_text,
# #     inputs=[
# #         gr.inputs.Textbox(label="Input Text"),
# #     ],
# #     outputs=gr.inputs.Textbox(label="Generated Text"),
# #     title="Falcon-7B Instruct",
# #     image=logo
# # ).launch()


from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
import torch


title = "🦅Falcon 🗨️ChatBot"
description = "Falcon-RW-1B is a 1B parameters causal decoder-only model built by TII and trained on 350B tokens of RefinedWeb."
examples = [["How are you?"]]


tokenizer = AutoTokenizer.from_pretrained("Cogwisechat/falcon-7b-finance")
model = AutoModelForCausalLM.from_pretrained(
    "Cogwisechat/falcon-7b-finance",
    trust_remote_code=True,
)


def predict(input, history=[]):
    # tokenize the new input sentence
    new_user_input_ids = tokenizer.encode(
        input + tokenizer.eos_token, return_tensors="pt"
    )

    # append the new user input tokens to the chat history
    bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)

    # generate a response
    history = model.generate(
        bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
    ).tolist()

    # convert the tokens to text, and then split the responses into lines
    response = tokenizer.decode(history[0]).split("<|endoftext|>")
    # print('decoded_response-->>'+str(response))
    response = [
        (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
    ]  # convert to tuples of list
    # print('response-->>'+str(response))
    return response, history


gr.Interface(
    fn=predict,
    title=title,
    description=description,
    examples=examples,
    inputs=["text", "state"],
    outputs=["chatbot", "state"],
    theme="finlaymacklon/boxy_violet",
).launch()