Codeassist / app.py
girishwangikar's picture
Create app.py
7d6ea64 verified
raw
history blame
No virus
2.93 kB
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()