Kiy-K commited on
Commit
8273e0b
·
verified ·
1 Parent(s): 6d37435

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +192 -57
app.py CHANGED
@@ -1,70 +1,205 @@
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
 
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- messages.extend(history)
22
-
23
- messages.append({"role": "user", "content": message})
 
24
 
25
- response = ""
 
 
 
 
 
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
 
39
- response += token
40
- yield response
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
- )
62
 
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- if __name__ == "__main__":
70
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
+ import random
5
 
6
+ def get_coding_samples():
7
+ """Diverse coding challenges"""
8
+ return [
9
+ "Write a function to reverse a linked list",
10
+ "Implement binary search in Python",
11
+ "Create a REST API endpoint for user authentication",
12
+ "Write a SQL query to find duplicate records",
13
+ "Build a React component for a todo list",
14
+ "Implement quicksort algorithm",
15
+ "Parse JSON and handle errors gracefully",
16
+ "Create a decorator for timing function execution",
17
+ "Write regex to validate email addresses",
18
+ "Implement a LRU cache in Python",
19
+ "Build a simple web scraper with BeautifulSoup",
20
+ "Create a custom hook in React",
21
+ "Write unit tests for a calculator function",
22
+ "Implement depth-first search for a graph",
23
+ "Build a CLI tool with argparse",
24
+ "Create a database migration script",
25
+ "Write a function to detect palindromes",
26
+ "Implement JWT token authentication",
27
+ "Build a responsive navbar with Tailwind CSS",
28
+ "Create a rate limiter middleware",
29
+ ]
30
 
31
+ def create_coder_system_message():
32
+ """System prompt for coding assistance"""
33
+ return """You are CoderAI, an expert programming assistant.
 
 
 
 
 
 
 
 
 
 
34
 
35
+ **Your Approach:**
36
+ 1. Understand the requirements clearly
37
+ 2. Provide clean, working code with comments
38
+ 3. Explain key concepts and design decisions
39
+ 4. Suggest best practices and optimizations
40
+ 5. Include error handling where appropriate
41
 
42
+ **Code Quality:**
43
+ - Write readable, maintainable code
44
+ - Follow language conventions and style guides
45
+ - Use meaningful variable names
46
+ - Add docstrings/comments for complex logic
47
+ - Consider edge cases and error handling
48
 
49
+ **Format:**
50
+ - Use markdown code blocks with language tags
51
+ - Explain code sections when helpful
52
+ - Provide usage examples
53
+ - Mention dependencies if needed
 
 
 
 
 
 
54
 
55
+ Be practical, efficient, and educational."""
 
56
 
57
+ def respond(message, history, system_message, max_tokens, temperature, top_p, model_choice):
58
+ """Streaming response with error handling"""
59
+ hf_token = os.getenv("HF_TOKEN")
60
+ if not hf_token:
61
+ yield "❌ **Error:** HF_TOKEN not found. Set it in environment or Spaces secrets."
62
+ return
63
+
64
+ # Model selection
65
+ models = {
66
+ "DeepSeek Coder": "deepseek-ai/deepseek-coder-33b-instruct",
67
+ "CodeLlama": "codellama/CodeLlama-34b-Instruct-hf",
68
+ "Qwen Coder": "Qwen/Qwen2.5-Coder-32B-Instruct",
69
+ }
70
+
71
+ client = InferenceClient(
72
+ model=models.get(model_choice, models["Qwen Coder"]),
73
+ token=hf_token
74
+ )
75
+
76
+ messages = [{"role": "system", "content": system_message}]
77
+
78
+ for msg in history:
79
+ if isinstance(msg, dict):
80
+ role = msg.get("role", "user")
81
+ content = msg.get("content", "")
82
+ else:
83
+ role = "user"
84
+ content = str(msg)
85
+
86
+ if content:
87
+ messages.append({"role": role, "content": content})
88
+
89
+ messages.append({"role": "user", "content": message})
90
+
91
+ try:
92
+ response_text = ""
93
+ for chunk in client.chat_completion(
94
+ messages,
95
+ max_tokens=max_tokens,
96
+ temperature=temperature,
97
+ top_p=top_p,
98
+ stream=True
99
+ ):
100
+ if chunk.choices[0].delta.content:
101
+ response_text += chunk.choices[0].delta.content
102
+ yield response_text
103
+
104
+ except Exception as e:
105
+ error_msg = f"❌ **Error:** {str(e)}\n\nTry:\n- Checking your HF_TOKEN\n- Simplifying the request\n- Using a different model"
106
+ yield error_msg
107
 
108
+ def get_random_sample():
109
+ """Get random coding challenge"""
110
+ return random.choice(get_coding_samples())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
+ # Gradio Interface
113
+ with gr.Blocks(title="💻 CoderAI", theme=gr.themes.Soft()) as demo:
114
+ gr.Markdown("# 💻 **CoderAI**\n*Your AI Programming Assistant*")
115
+
116
+ chatbot = gr.Chatbot(
117
+ height=500,
118
+ type='messages',
119
+ label="💬 Conversation",
120
+ show_copy_button=True
121
+ )
122
+
123
+ msg = gr.Textbox(
124
+ placeholder="Ask a coding question or describe what you need...",
125
+ show_label=False,
126
+ scale=4
127
+ )
128
+
129
+ with gr.Row():
130
+ submit = gr.Button("🚀 Code", variant="primary", scale=1)
131
+ clear = gr.Button("🗑️ Clear", variant="secondary", scale=1)
132
+ sample = gr.Button("🎲 Random", variant="secondary", scale=1)
133
+
134
+ with gr.Accordion("⚙️ Advanced Settings", open=False):
135
+ model_dropdown = gr.Dropdown(
136
+ choices=["Qwen Coder", "DeepSeek Coder", "CodeLlama"],
137
+ value="Qwen Coder",
138
+ label="Model Selection"
139
+ )
140
+ temp_slider = gr.Slider(0.1, 1.0, value=0.2, step=0.1, label="Temperature")
141
+ tokens_slider = gr.Slider(512, 4096, value=2048, step=256, label="Max Tokens")
142
+ top_p_slider = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p")
143
+
144
+ with gr.Accordion("💡 Help & Examples", open=False):
145
+ gr.Markdown("""
146
+ **Tips:**
147
+ - Be specific about language and requirements
148
+ - Mention if you need comments or tests
149
+ - Ask for explanations of complex parts
150
+ - Request specific design patterns or styles
151
 
152
+ **What I can help with:**
153
+ - Writing functions and classes
154
+ - Debugging code
155
+ - Code review and optimization
156
+ - Algorithm implementation
157
+ - API design and database queries
158
+ - Testing and documentation
159
+ """)
160
+
161
+ gr.Examples(
162
+ examples=[
163
+ ["Write a Python function to check if a string is a palindrome"],
164
+ ["Create a React component for a searchable dropdown"],
165
+ ["Implement a binary tree traversal in JavaScript"],
166
+ ["Write a SQL query to find the top 5 customers by revenue"],
167
+ ["Build a simple Flask API with error handling"],
168
+ ["Create a custom validation decorator in Python"],
169
+ ],
170
+ inputs=msg
171
+ )
172
+
173
+ system_msg = gr.State(create_coder_system_message())
174
+
175
+ def chat_response(message, history, sys_msg, max_tok, temp, top_p, model):
176
+ """Handle chat with streaming"""
177
+ if not message.strip():
178
+ return history, ""
179
+
180
+ history.append({"role": "user", "content": message})
181
+ history.append({"role": "assistant", "content": ""})
182
+
183
+ for response in respond(message, history[:-1], sys_msg, max_tok, temp, top_p, model):
184
+ history[-1]["content"] = response
185
+ yield history, ""
186
+
187
+ return history, ""
188
+
189
+ def clear_chat():
190
+ return [], ""
191
+
192
+ msg.submit(
193
+ chat_response,
194
+ [msg, chatbot, system_msg, tokens_slider, temp_slider, top_p_slider, model_dropdown],
195
+ [chatbot, msg]
196
+ )
197
+ submit.click(
198
+ chat_response,
199
+ [msg, chatbot, system_msg, tokens_slider, temp_slider, top_p_slider, model_dropdown],
200
+ [chatbot, msg]
201
+ )
202
+ clear.click(clear_chat, outputs=[chatbot, msg])
203
+ sample.click(get_random_sample, outputs=msg)
204
 
205
+ demo.launch()