File size: 1,141 Bytes
13d0b4d
722177d
921a7bd
722177d
921a7bd
 
3aaf394
 
19e4851
13d0b4d
de1935d
921a7bd
 
de1935d
 
 
 
 
 
921a7bd
80ca826
de1935d
626064c
921a7bd
de1935d
626064c
 
de1935d
921a7bd
19e4851
921a7bd
de1935d
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
import spaces
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load model and tokenizer
model_name = "infly/OpenCoder-8B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)

@spaces.GPU
# Define the text generation function
def generate_text(prompt):
    inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True)
    outputs = model.generate(
        inputs["input_ids"],
        attention_mask=inputs["attention_mask"],  # Add attention mask
        max_length=50,  # Reduce max_length to conserve memory
        num_return_sequences=1
    )
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# Create the Gradio interface
iface = gr.Interface(
    fn=generate_text,
    inputs=gr.Textbox(label="Enter your prompt", placeholder="Start typing...", lines=5),
    outputs="text",
    title="OpenCoder 8B Instruct",
    description="Generate text using the OpenCoder model. Input a prompt to generate responses.",
)

# Launch the Gradio app
iface.launch()