Spaces:
Sleeping
Sleeping
File size: 3,736 Bytes
7d6ea64 b56253f 7d6ea64 433c5a7 ea1244f 1d8ad9a ea1244f 1d8ad9a ea1244f a168c8f 7d6ea64 fe0a93c 7d6ea64 4dd0399 7d6ea64 4dd0399 7d6ea64 4dd0399 7d6ea64 4dd0399 7d6ea64 ea1244f 7d6ea64 fe0a93c a66c6e1 433c5a7 7d6ea64 a66c6e1 7d6ea64 433c5a7 7d6ea64 fe0a93c 7d6ea64 fe0a93c 433c5a7 7d6ea64 f83d803 6996122 |
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import os
import time
import gradio as gr
from groq import Groq
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
client = Groq(api_key=GROQ_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, p, h1 {
text-align: center;
color: white;
}
footer {
text-align: center;
padding: 10px;
width: 100%;
background-color: rgba(240, 240, 240, 0.8);
z-index: 1000;
position: relative;
margin-top: 10px;
color: black;
}
"""
FOOTER_TEXT = """<footer style="text-align: center;">
<p>To learn more about this app, visit my <a href="https://medium.com/@girishwangikar/codeassist-ai-59984dcc3b40" target="_blank">blog</a>.<br>
If you enjoyed the functionality of the app, please leave a like!<br>
Check out more on <a href="https://www.linkedin.com/in/girish-wangikar/" target="_blank">LinkedIn</a> |
<a href="https://girishwangikar.github.io/Girish_Wangikar_Portfolio.github.io/" target="_blank">Portfolio</a></p>
</footer>"""
def generate_response(
message: str,
history: list,
system_prompt: str,
temperature: float = 0.5,
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)
with gr.Accordion("⚙️ Parameters", open=False):
system_prompt = gr.Textbox(
value="You are a helpful coding assistant, specialized in code completion, debugging, and analysis. Provide concise and accurate responses.",
label="System Prompt",
)
temperature = gr.Slider(
minimum=0,
maximum=1,
step=0.1,
value=0.5,
label="Temperature",
)
max_tokens = gr.Slider(
minimum=50,
maximum=1024,
step=1,
value=512,
label="Max tokens",
)
chat_interface = gr.ChatInterface(
fn=generate_response,
chatbot=chatbot,
additional_inputs=[system_prompt, temperature, max_tokens],
examples=[
["What are Python generators, and how do I use them effectively in my code?"],
["Can you explain the different types of SQL joins with examples?"],
["How do I calculate the time complexity of a function? Explain with example"],
["Explain this code to me: [paste your code here]"],
],
cache_examples=False,
)
clear_btn = gr.Button("Clear Conversation")
clear_btn.click(clear_conversation, outputs=[chatbot, chatbot])
gr.HTML(FOOTER_TEXT)
if __name__ == "__main__":
demo.launch(share=True)
|