File size: 1,709 Bytes
9f8b574
c41c45a
ab3d718
c41c45a
ec8cf8a
9ac8791
9f8b574
c41c45a
c15417b
 
 
 
 
ec8cf8a
 
 
 
 
0357904
77ab936
ec8cf8a
0357904
9ac8791
 
 
ec8cf8a
 
 
0357904
 
 
9ac8791
0357904
ec8cf8a
7b95784
ec8cf8a
 
 
c41c45a
9ac8791
ec8cf8a
9ac8791
c15417b
 
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
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
import torch

# Load a model suited for code generation
model_name = "Salesforce/codegen-350M-mono"  # Choose a suitable model for your needs
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Set a padding token if it doesn't exist
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token  # Set pad_token to eos_token

model = AutoModelForCausalLM.from_pretrained(model_name)
# Set the device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

def generate_code(prompt):
    # Refined prompt
    full_prompt = f"Please generate a basic HTML template for a personal blog, including a header, main content area for posts, and a footer. The output should be only the HTML code."
    
    # Tokenize the input
    input_tensor = tokenizer(full_prompt, return_tensors="pt", padding=True, truncation=True).to(device)
    
    # Generate code with attention mask
    with torch.no_grad():
        generated_ids = model.generate(
            input_tensor['input_ids'],
            attention_mask=input_tensor['attention_mask'],
            max_length=500,  # Increase if necessary to capture full HTML
            num_beams=5,
            early_stopping=True,
            pad_token_id=tokenizer.pad_token_id
        )
    
    # Decode and return the generated code
    generated_code = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
    return generated_code

# Set up the Gradio interface
iface = gr.Interface(fn=generate_code, inputs="text", outputs="text", allow_flagging="never")

# Launch the app
iface.launch(server_name="0.0.0.0", server_port=7860)