SunderAli17 commited on
Commit
ca321f9
β€’
1 Parent(s): ecabbb5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ import spaces
5
+
6
+ title = """# 🀡Welcome to SAK's CodBot πŸ’» (A Coding ChatBot) ⭐⭐⭐"""
7
+ description = """CodingBot is an open-source coding language model that delivers excellent performance.
8
+
9
+ CodingBot leverages LlamaforCausalLM.
10
+
11
+ CodingBot supports 'java', 'javascript', 'c++', 'c#', 'c', 'html', 'java_server_pages', 'python', 'php', 'go', 'kotlin', 'swift', 'dart', 'shell', 'json', 'lua', 'matlab', 'yaml', 'css', 'rust', 'sql', 'ruby', 'tex', 'objective-c', 'powershell', 'ocaml', 'groovy', 'cmake', 'julia', 'perl', 'assembly', 'haskell', 'fortran', 'pascal', 'rmarkdown', 'scala', 'visual_basic', 'verilog', 'prolog', 'r', 'dockerfile','cobol', 'batchfile', 'toml', 'lisp', 'erlang', 'coffeescript', 'makefile', 'clojure', 'elixir'
12
+ ```
13
+ **Demo by [Sunder Ali Khowaja](https://sander-ali.github.io) - [X](https://x.com/SunderAKhowaja) -[Github](https://github.com/sander-ali) -[Hugging Face](https://huggingface.co/SunderAli17)**
14
+ """
15
+
16
+ # Define the device and model path
17
+ device = "cuda" if torch.cuda.is_available() else "cpu"
18
+ model_path = "SunderAli17/CodingBot"
19
+
20
+ # Load the tokenizer and model
21
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
22
+ model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto").eval()
23
+
24
+ @spaces.GPU(duration=130)
25
+ def generate_code(system_prompt, user_prompt, max_length):
26
+ messages = [
27
+ {"role": "system", "content": system_prompt},
28
+ {"role": "user", "content": user_prompt}
29
+ ]
30
+ text = tokenizer.apply_chat_template(
31
+ messages,
32
+ tokenize=False,
33
+ add_generation_prompt=True
34
+ )
35
+ model_inputs = tokenizer([text], return_tensors="pt").to(device)
36
+
37
+ generated_ids = model.generate(
38
+ model_inputs.input_ids,
39
+ max_new_tokens=max_length,
40
+ eos_token_id=tokenizer.eos_token_id
41
+ )
42
+ generated_ids = [
43
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
44
+ ]
45
+
46
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
47
+ return response
48
+
49
+ theme = gr.themes.Soft(
50
+ font=[gr.themes.GoogleFont('Source Code Pro'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
51
+ )
52
+ js_func = """
53
+ function refresh() {
54
+ const url = new URL(window.location);
55
+ if (url.searchParams.get('__theme') !== 'dark') {
56
+ url.searchParams.set('__theme', 'dark');
57
+ window.location.href = url.href;
58
+ }
59
+ }
60
+ """
61
+ with gr.Blocks(js = js_func, theme = theme) as SAK:
62
+ gr.Markdown(title)
63
+ gr.Markdown(description)
64
+
65
+ system_prompt_input = gr.Textbox(
66
+ label="πŸ‘¨β€πŸ’» CodBot Instruction:",
67
+ value="Hello Sir! how are you today? I am here to generate clear and concise code examples for you.",
68
+ lines=2
69
+ )
70
+ user_prompt_input = gr.Code(
71
+ label="❓ Coding Prompt πŸ’»",
72
+ value="Write an object detection code using YOLOv10 in Python",
73
+ language="python",
74
+ lines=15
75
+ )
76
+ code_output = gr.Code(label="πŸ‘¨β€πŸ’» CodBot", language='python', lines=50, interactive=True)
77
+ max_length_slider = gr.Slider(minimum=1, maximum=1800, value=650, label="Max Token Length")
78
+
79
+ generate_button = gr.Button("Generate Code")
80
+ generate_button.click(
81
+ generate_code,
82
+ inputs=[system_prompt_input, user_prompt_input, max_length_slider],
83
+ outputs=code_output
84
+ )
85
+
86
+ SAK.queue().launch(debug=True, share=True)