File size: 2,930 Bytes
7d6ea64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import time
import gradio as gr
from groq import Groq

API_KEY = os.environ.get("GROQ_API_KEY")
client = Groq(api_key=API_KEY)

TITLE = "<h1><center>CodeAssist AI</center></h1>"

PLACEHOLDER = """
<center>
<p>Hi, I'm your coding assistant. Ask me anything about programming!</p>
</center>
"""

CSS = """
.duplicate-button {
    margin: auto !important;
    color: white !important;
    background: black !important;
    border-radius: 100vh !important;
}
h3 {
    text-align: center;
}
"""

def generate_response(
    message: str, 
    history: list,
    system_prompt: str,
    temperature: float = 0.7, 
    max_tokens: int = 512
):
    conversation = [
        {"role": "system", "content": system_prompt}
    ]
    for prompt, answer in history:
        conversation.extend([
            {"role": "user", "content": prompt}, 
            {"role": "assistant", "content": answer},
        ])

    conversation.append({"role": "user", "content": message})

    response = client.chat.completions.create(
        model="llama-3.1-8B-Instant",
        messages=conversation,
        temperature=temperature,
        max_tokens=max_tokens,
        stream=True
    )

    partial_message = ""
    for chunk in response:
        if chunk.choices[0].delta.content is not None:
            partial_message += chunk.choices[0].delta.content
            yield partial_message

def clear_conversation():
    return [], None

chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)

with gr.Blocks(css=CSS, theme="Nymbo/Nymbo_Theme") as demo:
    gr.HTML(TITLE)
    gr.ChatInterface(
        fn=generate_response,
        chatbot=chatbot,
        fill_height=True,
        additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False),
        additional_inputs=[
            gr.Textbox(
                value="You are a helpful coding assistant, specialized in code completion, debugging, and analysis. Provide concise and accurate responses.",
                label="System Prompt",
            ),
            gr.Slider(
                minimum=0,
                maximum=1,
                step=0.1,
                value=0.7,
                label="Temperature",
            ),
            gr.Slider(
                minimum=50,
                maximum=1024,
                step=1,
                value=512,
                label="Max tokens",
            ),
        ],
        examples=[
            ["How do I implement a binary search in Python?"],
            ["Explain the concept of recursion and provide a simple example."],
            ["What are the best practices for error handling in JavaScript?"],
            ["How can I optimize this code snippet: [paste your code here]"],
        ],
        cache_examples=False,
    )
    
    clear_btn = gr.Button("Clear Conversation")
    clear_btn.click(clear_conversation, outputs=[chatbot, chatbot])

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