Spaces:
Running
Running
girishwangikar
commited on
Commit
•
7d6ea64
1
Parent(s):
c358f9c
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import gradio as gr
|
4 |
+
from groq import Groq
|
5 |
+
|
6 |
+
API_KEY = os.environ.get("GROQ_API_KEY")
|
7 |
+
client = Groq(api_key=API_KEY)
|
8 |
+
|
9 |
+
TITLE = "<h1><center>CodeAssist AI</center></h1>"
|
10 |
+
|
11 |
+
PLACEHOLDER = """
|
12 |
+
<center>
|
13 |
+
<p>Hi, I'm your coding assistant. Ask me anything about programming!</p>
|
14 |
+
</center>
|
15 |
+
"""
|
16 |
+
|
17 |
+
CSS = """
|
18 |
+
.duplicate-button {
|
19 |
+
margin: auto !important;
|
20 |
+
color: white !important;
|
21 |
+
background: black !important;
|
22 |
+
border-radius: 100vh !important;
|
23 |
+
}
|
24 |
+
h3 {
|
25 |
+
text-align: center;
|
26 |
+
}
|
27 |
+
"""
|
28 |
+
|
29 |
+
def generate_response(
|
30 |
+
message: str,
|
31 |
+
history: list,
|
32 |
+
system_prompt: str,
|
33 |
+
temperature: float = 0.7,
|
34 |
+
max_tokens: int = 512
|
35 |
+
):
|
36 |
+
conversation = [
|
37 |
+
{"role": "system", "content": system_prompt}
|
38 |
+
]
|
39 |
+
for prompt, answer in history:
|
40 |
+
conversation.extend([
|
41 |
+
{"role": "user", "content": prompt},
|
42 |
+
{"role": "assistant", "content": answer},
|
43 |
+
])
|
44 |
+
|
45 |
+
conversation.append({"role": "user", "content": message})
|
46 |
+
|
47 |
+
response = client.chat.completions.create(
|
48 |
+
model="llama-3.1-8B-Instant",
|
49 |
+
messages=conversation,
|
50 |
+
temperature=temperature,
|
51 |
+
max_tokens=max_tokens,
|
52 |
+
stream=True
|
53 |
+
)
|
54 |
+
|
55 |
+
partial_message = ""
|
56 |
+
for chunk in response:
|
57 |
+
if chunk.choices[0].delta.content is not None:
|
58 |
+
partial_message += chunk.choices[0].delta.content
|
59 |
+
yield partial_message
|
60 |
+
|
61 |
+
def clear_conversation():
|
62 |
+
return [], None
|
63 |
+
|
64 |
+
chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)
|
65 |
+
|
66 |
+
with gr.Blocks(css=CSS, theme="Nymbo/Nymbo_Theme") as demo:
|
67 |
+
gr.HTML(TITLE)
|
68 |
+
gr.ChatInterface(
|
69 |
+
fn=generate_response,
|
70 |
+
chatbot=chatbot,
|
71 |
+
fill_height=True,
|
72 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False),
|
73 |
+
additional_inputs=[
|
74 |
+
gr.Textbox(
|
75 |
+
value="You are a helpful coding assistant, specialized in code completion, debugging, and analysis. Provide concise and accurate responses.",
|
76 |
+
label="System Prompt",
|
77 |
+
),
|
78 |
+
gr.Slider(
|
79 |
+
minimum=0,
|
80 |
+
maximum=1,
|
81 |
+
step=0.1,
|
82 |
+
value=0.7,
|
83 |
+
label="Temperature",
|
84 |
+
),
|
85 |
+
gr.Slider(
|
86 |
+
minimum=50,
|
87 |
+
maximum=1024,
|
88 |
+
step=1,
|
89 |
+
value=512,
|
90 |
+
label="Max tokens",
|
91 |
+
),
|
92 |
+
],
|
93 |
+
examples=[
|
94 |
+
["How do I implement a binary search in Python?"],
|
95 |
+
["Explain the concept of recursion and provide a simple example."],
|
96 |
+
["What are the best practices for error handling in JavaScript?"],
|
97 |
+
["How can I optimize this code snippet: [paste your code here]"],
|
98 |
+
],
|
99 |
+
cache_examples=False,
|
100 |
+
)
|
101 |
+
|
102 |
+
clear_btn = gr.Button("Clear Conversation")
|
103 |
+
clear_btn.click(clear_conversation, outputs=[chatbot, chatbot])
|
104 |
+
|
105 |
+
if __name__ == "__main__":
|
106 |
+
demo.launch()
|