File size: 1,256 Bytes
26e78ff
217a305
26e78ff
217a305
26e78ff
 
 
 
5b195fc
26e78ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebb322e
217a305
c05a9c3
b52a718
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
import re
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained(
    'parsak/codegen-350M-mono-lora-instruction',
)
tokenizer = AutoTokenizer.from_pretrained('Salesforce/codegen-350M-mono')

tokenizer.pad_token_id = 0  # different to <eos>
tokenizer.padding_side = "left"  # Allow batched inference

def extract_code(input_text):
    pattern = r"'''py\n(.*?)'''"
    match = re.search(pattern, input_text, re.DOTALL)
    
    if match:
        return match.group(1)
    else:
        return None  # Return None if no match is found

def generate_code(input_text):
    input_ids = tokenizer(input_text, return_tensors="pt").input_ids
    generated_ids = model.generate(input_ids, max_length=128)
    result = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
    return extract_code(result)

def respond(message, chat_history, additional_inputs):
    return  f"Here's an example code:\n\n```python\n{generate_code(message)}\n```" 






gr.ChatInterface(respond,
        retry_btn= gr.Button(value="Retry"), 
        undo_btn=None, clear_btn=gr.Button(value="Clear"),
        additional_inputs=[
            gr.Dropdown(["annen", "baban"])
        ]
        ).launch()