raj9305 commited on
Commit
85fbabb
·
verified ·
1 Parent(s): 0d1adb1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
+ import gradio as gr
4
+
5
+ model_name = "codellama/CodeLlama-7b-Instruct-hf"
6
+ print("Loading model...")
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name,
10
+ torch_dtype=torch.float16,
11
+ device_map="auto")
12
+ print("Model loaded.")
13
+
14
+ generator = pipeline(
15
+ "text-generation",
16
+ model=model,
17
+ tokenizer=tokenizer,
18
+ temperature=0.1,
19
+ top_p=0.95,
20
+ max_new_tokens=512,
21
+ repetition_penalty=1.05)
22
+
23
+ def format_prompt(chat):
24
+ prompt = ""
25
+ for user_msg, ai_reply in chat:
26
+ prompt += f"<s>[INST] {user_msg.strip()} [/INST] {ai_reply.strip()}</s>\n"
27
+ return prompt
28
+
29
+ def chat_fn(user_input, history):
30
+ history = history or []
31
+ prompt = format_prompt(history + [[user_input, ""]])
32
+ generated = generator(prompt, do_sample=True)[0]["generated_text"]
33
+ answer = generated[len(prompt):].strip()
34
+ history.append((user_input, answer))
35
+ return "", history
36
+
37
+ with gr.Blocks() as demo:
38
+ gr.Markdown("# 🦙 CodeLlama Copilot\nFree & private code assistant.")
39
+ chatbot = gr.Chatbot(label="Developer Assistant").style(height=400)
40
+ with gr.Row():
41
+ msg = gr.Textbox(placeholder="Ask me coding questions", show_label=False).style(container=False)
42
+ clear = gr.Button("🔄 Clear Conversation")
43
+ msg.submit(chat_fn, [msg, chatbot], [msg, chatbot])
44
+ clear.click(lambda: ("", []), None, [msg, chatbot])
45
+
46
+ if __name__ == "__main__":
47
+ demo.launch()