Spaces:
Runtime error
Runtime error
File size: 1,761 Bytes
7d244cb |
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 |
import gradio as gr
import spaces
import os
import spaces
import torch
import random
import time
import re
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
# Set an environment variable
HF_TOKEN = os.environ.get("HF_TOKEN", None)
zero = torch.Tensor([0]).cuda()
print(zero.device) # <-- 'cpu' 🤔
model_id = 'FINGU-AI/Finance-OrpoMistral-7B' #attn_implementation="flash_attention_2",
model = AutoModelForCausalLM.from_pretrained(model_id,attn_implementation="sdpa", torch_dtype= torch.bfloat16)
tokenizer = AutoTokenizer.from_pretrained(model_id)
model.to('cuda')
# terminators = [
# tokenizer.eos_token_id,
# tokenizer.convert_tokens_to_ids("<|eot_id|>")
# ]
generation_params = {
'max_new_tokens': 1000,
'use_cache': True,
'do_sample': True,
'temperature': 0.7,
'top_p': 0.9,
'top_k': 50,
}
@spaces.GPU
def inference(query):
messages = [
{"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."""},
{"role": "user", "content": f"{query}"},
]
tokenized_chat = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to("cuda")
outputs = model.generate(tokenized_chat, **generation_params)
decoded_outputs = tokenizer.batch_decode(outputs)
assistant_response = decoded_outputs[0].split("Assistant:")[-1].strip()
return assistant_response
def response(message, history):
text = inference(message)
for i in range(len(text)):
time.sleep(0.01)
yield text[: i + 1]
gr.ChatInterface(response).launch() |