File size: 1,651 Bytes
c2283c0
890da44
c2283c0
 
890da44
 
c2283c0
890da44
 
c2283c0
890da44
 
c2283c0
 
 
 
890da44
 
 
 
 
 
 
 
c2283c0
 
 
890da44
 
 
 
 
 
 
 
 
 
 
c2283c0
890da44
 
 
 
c2283c0
890da44
 
c2283c0
890da44
c2283c0
890da44
 
 
 
 
 
c2283c0
 
890da44
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
48
49
50
51
52
53
54
55
56
57
58
59
import gradio as gr
import os
from huggingface_hub import InferenceClient

# Get the token from the "HF_TOKEN" environment variable
token = os.getenv("HF_TOKEN")

# Create a client for the model using the token
client = InferenceClient("meta-llama/Llama-3.1-8B-Instruct", token=token)

def generate_code(
    task_description,
    max_tokens,
    temperature,
    top_p,
):
    # 2. Create a prompt using task description
    prompt = task_description

    # 3. Generate code based on the description
    generated_code = ""
    for message in client.text_generation(
        prompt,
        max_new_tokens=max_tokens,
        temperature=temperature,
        top_p=top_p,
    ):
        generated_code += message
        yield generated_code

# 4. Create Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# πŸš€ Llama Code Generator")

    with gr.Row():
        task_input = gr.Textbox(
            lines=3, placeholder="Describe the task in natural language...", label="Task Description"
        )

    with gr.Row():
        max_tokens = gr.Slider(1, 2048, value=100, step=1, label="Max Tokens")
        temperature = gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature")
        top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")

    with gr.Row():
        submit_button = gr.Button("Generate Code πŸš€")

    output = gr.Textbox(lines=10, label="Generated Code")

    # 5. Button click triggers code generation
    submit_button.click(
        generate_code,
        inputs=[task_input, max_tokens, temperature, top_p],
        outputs=output,
    )

if __name__ == "__main__":
    demo.launch(show_error=True)