hikinegi commited on
Commit
5fde9f6
Β·
1 Parent(s): e9b86b3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # load model once
2
+ import torch
3
+ from peft import AutoPeftModelForCausalLM
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+
6
+ model_id = "hikinegi/Llama-JAVA_tuned"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
8
+ model = AutoPeftModelForCausalLM.from_pretrained(model_id, device_map='auto', torch_dtype=torch.float16)
9
+
10
+ # Set the model to evaluation mode
11
+ model.eval()
12
+
13
+ def generate_pred(text):
14
+ # Disable gradient calculation
15
+ with torch.no_grad():
16
+ # generate
17
+ text=f"<s>[INST]<<SYS>>\nBelow is an instruction that describes a task. Write a response that appropriately completes the request.\n<</SYS>>\n{text}[/INST]"
18
+ inputs = tokenizer(text, return_tensors="pt").to("cuda")
19
+ outputs = model.generate(input_ids=inputs["input_ids"],
20
+ attention_mask=inputs["attention_mask"],
21
+ max_new_tokens=1024,
22
+ pad_token_id=tokenizer.eos_token_id)
23
+ return (tokenizer.decode(outputs[0], skip_special_tokens=False))
24
+
25
+ import gradio as gr
26
+ import random
27
+ import time
28
+
29
+ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
30
+ gr.Markdown("""<h1><center>CodeGuru will answer all of your'e JAVA coding Question</center></h1> """)
31
+ chatbot = gr.Chatbot(label="CodeGuru")
32
+ msg = gr.Textbox(label = "Question")
33
+ clear = gr.ClearButton([msg, chatbot])
34
+
35
+ def user(user_message, history):
36
+ return "", history + [[user_message, None]]
37
+
38
+ def bot(history):
39
+ bot_message = generate_pred(history[-1][0])
40
+ history[-1][1] = ""
41
+ for character in bot_message:
42
+ history[-1][1] += character
43
+ time.sleep(0.05)
44
+ yield history
45
+
46
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
47
+ bot, chatbot, chatbot
48
+ )
49
+ clear.click(lambda: None, None, chatbot, queue=False)
50
+
51
+ with gr.Row(visible=True) as button_row:
52
+ upvote_btn = gr.Button(value="πŸ‘ Upvote", interactive=True)
53
+ downvote_btn = gr.Button(value="πŸ‘Ž Downvote", interactive=True)
54
+
55
+ demo.queue()
56
+ demo.launch(debug=True)