File size: 1,865 Bytes
de0a5cf
86e230c
 
c585826
 
 
 
 
 
 
35d2683
c585826
de0a5cf
58612e0
 
 
 
 
 
 
 
86e230c
 
de0a5cf
86e230c
 
 
23152a0
 
 
c585826
 
 
 
 
 
 
 
23152a0
86e230c
58612e0
 
 
 
 
3951dc7
58612e0
c585826
58612e0
c585826
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
import gradio as gr
from fpl_client import FPLClient
from nlp_utils import process_query
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/deepseek-coder-1.3b-instruct", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("deepseek-ai/deepseek-coder-1.3b-instruct", trust_remote_code=True, torch_dtype=torch.bfloat16)

# Use CPU if CUDA is not available
device = torch.device("cpu" if not torch.cuda.is_available() else "cuda")
model = model.to(device)

# Theme builder
# gr.themes.builder()

theme = gr.themes.Soft(
    primary_hue="sky",
    neutral_hue="slate",
)

# Initialize the FPL client
fpl_client = FPLClient()

# Function to handle user input and generate a response
def chatbot_response(query):
    response = process_query(query, fpl_client)
    # if response if a JSON boject iterate over the elements and conver is a list like "a": "b" "/n" "c": "d"
    if isinstance(response, dict):
        response = "\n".join([f"{key}: {value}" for key, value in response.items()])
    
    # Generate response using the model
    messages = [{'role': 'user', 'content': query}]
    inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
    outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, top_k=50, top_p=0.95, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
    model_response = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
    
    return response + "\n\n" + model_response

# Set up the Gradio interface
iface = gr.Interface(
    fn=chatbot_response,
    inputs=gr.Textbox(label="Ask our FPL Expert"),
    outputs=gr.Textbox(label="Hope it helps!"),
    theme=theme,
    title="FPL Chatbot"
)

if __name__ == "__main__":
    iface.launch()