Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
import os
|
4 |
+
import spaces
|
5 |
+
import torch
|
6 |
+
import random
|
7 |
+
import time
|
8 |
+
import re
|
9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
|
10 |
+
|
11 |
+
|
12 |
+
# Set an environment variable
|
13 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
14 |
+
|
15 |
+
zero = torch.Tensor([0]).cuda()
|
16 |
+
print(zero.device) # <-- 'cpu' 🤔
|
17 |
+
|
18 |
+
|
19 |
+
model_id = 'FINGU-AI/Finance-OrpoMistral-7B' #attn_implementation="flash_attention_2",
|
20 |
+
model = AutoModelForCausalLM.from_pretrained(model_id,attn_implementation="sdpa", torch_dtype= torch.bfloat16)
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
22 |
+
model.to('cuda')
|
23 |
+
|
24 |
+
# terminators = [
|
25 |
+
# tokenizer.eos_token_id,
|
26 |
+
# tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
27 |
+
# ]
|
28 |
+
|
29 |
+
generation_params = {
|
30 |
+
'max_new_tokens': 1000,
|
31 |
+
'use_cache': True,
|
32 |
+
'do_sample': True,
|
33 |
+
'temperature': 0.7,
|
34 |
+
'top_p': 0.9,
|
35 |
+
'top_k': 50,
|
36 |
+
}
|
37 |
+
|
38 |
+
@spaces.GPU
|
39 |
+
def inference(query):
|
40 |
+
messages = [
|
41 |
+
{"role": "system", "content": """You are a friendly AI assistant named Grinda, specialized in assisting users with trade, stock-related queries. Your tasks include providing insightful suggestions, tips, and winning trade strategies."""},
|
42 |
+
{"role": "user", "content": f"{query}"},
|
43 |
+
]
|
44 |
+
|
45 |
+
tokenized_chat = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to("cuda")
|
46 |
+
|
47 |
+
outputs = model.generate(tokenized_chat, **generation_params)
|
48 |
+
decoded_outputs = tokenizer.batch_decode(outputs)
|
49 |
+
assistant_response = decoded_outputs[0].split("Assistant:")[-1].strip()
|
50 |
+
return assistant_response
|
51 |
+
|
52 |
+
|
53 |
+
def response(message, history):
|
54 |
+
text = inference(message)
|
55 |
+
for i in range(len(text)):
|
56 |
+
time.sleep(0.01)
|
57 |
+
yield text[: i + 1]
|
58 |
+
gr.ChatInterface(response).launch()
|