HakanKilic01 commited on
Commit
26e78ff
1 Parent(s): 5b195fc
Files changed (1) hide show
  1. app.py +40 -10
app.py CHANGED
@@ -1,18 +1,48 @@
1
  import random
 
2
  import gradio as gr
 
 
3
 
4
- code = 'def hello_world(): return "Hello, world!" print(hello_world())'
 
 
 
5
 
6
- def prompt(message, history):
7
- return gr.Code(value=code,language="python")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- demo = gr.ChatInterface(prompt,
10
- title="codeBot",
11
- description="Ask me a question",
12
- retry_btn="Retry",
13
- undo_btn=None,
14
- clear_btn="Clear")
15
 
16
 
17
 
18
- demo.launch()
 
1
  import random
2
+ import re
3
  import gradio as gr
4
+ import requests
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer
6
 
7
+ model = AutoModelForCausalLM.from_pretrained(
8
+ 'parsak/codegen-350M-mono-lora-instruction',
9
+ )
10
+ tokenizer = AutoTokenizer.from_pretrained('Salesforce/codegen-350M-mono')
11
 
12
+ tokenizer.pad_token_id = 0 # different to <eos>
13
+ tokenizer.padding_side = "left" # Allow batched inference
14
+
15
+ def extract_code(input_text):
16
+ pattern = r"'''py\n(.*?)'''"
17
+ match = re.search(pattern, input_text, re.DOTALL)
18
+
19
+ if match:
20
+ return match.group(1)
21
+ else:
22
+ return None # Return None if no match is found
23
+
24
+ def generate_code(input_text):
25
+ input_ids = tokenizer(input_text, return_tensors="pt").input_ids
26
+ generated_ids = model.generate(input_ids, max_length=128)
27
+ result = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
28
+ return extract_code(result)
29
+
30
+ def respond(message, chat_history, additional_inputs):
31
+ return f"Here's an example code:\n\n```python\n{generate_code(message)}\n```"
32
+
33
+
34
+
35
+
36
+
37
+
38
+ gr.ChatInterface(respond,
39
+ retry_btn= gr.Button(value="Retry"),
40
+ undo_btn=None, clear_btn=gr.Button(value="Clear"),
41
+ additional_inputs=[
42
+ gr.Dropdown(["annen", "baban"])
43
+ ]
44
+ ).launch()
45
 
 
 
 
 
 
 
46
 
47
 
48