File size: 1,491 Bytes
a9bca27
a90d3f5
a9bca27
a90d3f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9bca27
 
 
a90d3f5
 
 
a9bca27
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM

# Define constants
MODEL_NAME = "Ct1tz/Codebert-Base-B2D4G5"
MAX_LENGTH = 512

# Load the tokenizer with error handling
try:
    tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, model_max_length=MAX_LENGTH, trust_remote_code=True)
    print(f"Tokenizer vocabulary size: {len(tokenizer)}")
    print(f"Tokenizer type: {tokenizer.__class__.__name__}")
except Exception as e:
    print(f"Error loading tokenizer: {e}")
    raise

# Load the model with error handling
try:
    # Load the model (using AutoModelForCausalLM for chat/generation tasks)
    model = AutoModelForCausalLM.from_pretrained(
        MODEL_NAME,
        trust_remote_code=True
    )
except Exception as e:
    print(f"Error loading model: {e}")
    raise

# Define a chat function
def chat(input_text, history=[]):
    inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True, max_length=MAX_LENGTH)
    outputs = model.generate(**inputs, max_new_tokens=50, do_sample=True)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    history.append((input_text, response))
    return history, history

# Create Gradio chat interface
interface = gr.ChatInterface(
    fn=chat,
    title="CodeBERT Chat",
    description="Chat with the CodeBERT model (Ct1tz/Codebert-Base-B2D4G5) for code-related tasks.",
    theme="soft"
)

# Launch the interface
if __name__ == "__main__":
    interface.launch()