Eeppa commited on
Commit
5999980
·
verified ·
1 Parent(s): 2bbf7f0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +235 -0
app.py ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from model_utils import CodeThinkingAssistant
3
+ import time
4
+
5
+ # Initialize the assistant
6
+ # IMPORTANT: Replace with your fine-tuned model ID after training
7
+ MODEL_ID = "meta-llama/Llama-3.2-1B-Instruct" # CHANGE THIS to your model after fine-tuning
8
+
9
+ print("🚀 Initializing Llama 3.2 Codex Assistant...")
10
+ assistant = CodeThinkingAssistant(MODEL_ID)
11
+ print("✅ Ready to help with coding!")
12
+
13
+ # Custom CSS for better UI
14
+ custom_css = """
15
+ <style>
16
+ .thinking-mode {
17
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
18
+ padding: 2px;
19
+ border-radius: 10px;
20
+ }
21
+ .fast-mode {
22
+ background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
23
+ padding: 2px;
24
+ border-radius: 10px;
25
+ }
26
+ .thinking-box {
27
+ background-color: #f0f4ff;
28
+ border-left: 4px solid #667eea;
29
+ padding: 10px;
30
+ margin: 10px 0;
31
+ border-radius: 5px;
32
+ font-family: monospace;
33
+ }
34
+ .code-box {
35
+ background-color: #1e1e1e;
36
+ color: #d4d4d4;
37
+ padding: 15px;
38
+ border-radius: 5px;
39
+ font-family: 'Courier New', monospace;
40
+ overflow-x: auto;
41
+ }
42
+ footer {
43
+ visibility: visible;
44
+ text-align: center;
45
+ margin-top: 20px;
46
+ font-size: 12px;
47
+ }
48
+ </style>
49
+ """
50
+
51
+ def format_code_output(code: str) -> str:
52
+ """Format code for display"""
53
+ return f'<div class="code-box"><pre>{code}</pre></div>'
54
+
55
+ def format_thinking_output(thinking: str) -> str:
56
+ """Format thinking process for display"""
57
+ return f'<div class="thinking-box">💭 <strong>Thinking process:</strong><br>{thinking}</div>'
58
+
59
+ def respond(
60
+ message: str,
61
+ history: list,
62
+ thinking_mode: bool,
63
+ reasoning_style: str,
64
+ temperature: float,
65
+ max_tokens: int
66
+ ):
67
+ """Main response function for Gradio chat"""
68
+
69
+ if not message.strip():
70
+ yield "Please enter a coding question or task."
71
+ return
72
+
73
+ # Show thinking indicator
74
+ yield "🤔 Thinking" + "." * 3
75
+
76
+ try:
77
+ if thinking_mode:
78
+ if reasoning_style == "Step-by-step thinking":
79
+ result = assistant.generate_with_thinking(
80
+ message,
81
+ max_thought_tokens=300,
82
+ max_code_tokens=max_tokens
83
+ )
84
+ else: # Chain-of-thought
85
+ result = assistant.generate_with_chain_of_thought(message)
86
+
87
+ # Format output with both thinking and code
88
+ formatted_output = ""
89
+ if result.get("thinking"):
90
+ formatted_output += format_thinking_output(result["thinking"])
91
+ if result.get("code"):
92
+ formatted_output += "\n\n" + format_code_output(result["code"])
93
+
94
+ yield formatted_output
95
+ else:
96
+ # Fast mode
97
+ code = assistant.generate_fast(message, max_tokens=max_tokens)
98
+ yield format_code_output(code)
99
+
100
+ except Exception as e:
101
+ yield f"❌ Error: {str(e)}\n\nPlease make sure the model is loaded correctly."
102
+
103
+ # Build the Gradio interface
104
+ with gr.Blocks(css=custom_css, title="Llama 3.2 Codex - AI Coding Assistant") as demo:
105
+
106
+ gr.Markdown("""
107
+ # 🤖 Llama 3.2 1B Codex
108
+
109
+ ### Your AI Pair Programmer with Thinking Mode
110
+
111
+ Built with Llama 3.2 - Specialized for code generation with explicit reasoning.
112
+ """)
113
+
114
+ with gr.Row():
115
+ with gr.Column(scale=2):
116
+ # Chat interface
117
+ chatbot = gr.Chatbot(
118
+ label="Code Assistant",
119
+ height=500,
120
+ bubble_full_width=False
121
+ )
122
+
123
+ with gr.Row():
124
+ msg = gr.Textbox(
125
+ label="Ask for code help",
126
+ placeholder="Example: 'Write a function to sort a list of dictionaries by a key' or 'Explain this algorithm...'",
127
+ scale=4
128
+ )
129
+ send_btn = gr.Button("Send", variant="primary", scale=1)
130
+
131
+ with gr.Row():
132
+ clear_btn = gr.Button("Clear Chat")
133
+ example_btn = gr.Button("Load Example")
134
+
135
+ with gr.Column(scale=1):
136
+ gr.Markdown("### ⚙️ Settings")
137
+
138
+ thinking_mode = gr.Checkbox(
139
+ label="🧠 Enable Thinking Mode",
140
+ value=True,
141
+ info="Shows reasoning process before generating code"
142
+ )
143
+
144
+ reasoning_style = gr.Radio(
145
+ choices=["Step-by-step thinking", "Chain-of-thought"],
146
+ value="Step-by-step thinking",
147
+ label="Reasoning style",
148
+ visible=True
149
+ )
150
+
151
+ temperature = gr.Slider(
152
+ minimum=0.1,
153
+ maximum=1.5,
154
+ value=0.7,
155
+ step=0.1,
156
+ label="Temperature (creativity)",
157
+ info="Lower = more focused, Higher = more creative"
158
+ )
159
+
160
+ max_tokens = gr.Slider(
161
+ minimum=100,
162
+ maximum=1500,
163
+ value=600,
164
+ step=50,
165
+ label="Max response length",
166
+ info="Maximum tokens in response"
167
+ )
168
+
169
+ gr.Markdown("""
170
+ ---
171
+ ### 📌 Tips
172
+ - **Thinking Mode ON**: Best for complex problems
173
+ - **Thinking Mode OFF**: Faster responses for simple code
174
+ - Be specific in your requests
175
+ - Ask for explanations or optimizations
176
+ """)
177
+
178
+ # Example prompts
179
+ examples = gr.Examples(
180
+ examples=[
181
+ "Write a Python function to find the longest common prefix in a list of strings",
182
+ "Implement a binary search tree with insert and search methods",
183
+ "Explain the difference between deep and shallow copy in Python with examples",
184
+ "Write a recursive function to generate all permutations of a string",
185
+ "Create a decorator that measures function execution time",
186
+ "Implement a simple URL shortener using dictionary",
187
+ "Write a function to check if two strings are anagrams",
188
+ "Create a class for a bank account with deposit, withdraw, and interest calculation"
189
+ ],
190
+ inputs=msg,
191
+ label="Example Prompts (click to try)"
192
+ )
193
+
194
+ # Event handlers
195
+ def respond_wrapper(message, history, thinking_mode, reasoning_style, temperature, max_tokens):
196
+ response_generator = respond(message, history, thinking_mode, reasoning_style, temperature, max_tokens)
197
+ for response in response_generator:
198
+ history.append((message, response))
199
+ yield history, ""
200
+ # Reset after yielding
201
+ history = []
202
+ yield history, ""
203
+
204
+ # Wire up the events
205
+ send_btn.click(
206
+ respond,
207
+ [msg, chatbot, thinking_mode, reasoning_style, temperature, max_tokens],
208
+ [chatbot, msg]
209
+ )
210
+
211
+ msg.submit(
212
+ respond,
213
+ [msg, chatbot, thinking_mode, reasoning_style, temperature, max_tokens],
214
+ [chatbot, msg]
215
+ )
216
+
217
+ clear_btn.click(lambda: None, None, chatbot, queue=False)
218
+
219
+ example_btn.click(
220
+ lambda: "Write a function to check if a string is a palindrome (ignoring spaces, punctuation, and case)",
221
+ None,
222
+ msg
223
+ )
224
+
225
+ # Footer with required attribution
226
+ gr.Markdown("""
227
+ ---
228
+ <footer>
229
+ <b>Built with Llama</b> • Llama 3.2 1B Codex • <a href="https://llama.meta.com/" target="_blank">Meta Llama 3.2</a><br>
230
+ Licensed under <a href="/LICENSE.txt">Llama 3.2 Community License</a>
231
+ </footer>
232
+ """)
233
+
234
+ if __name__ == "__main__":
235
+ demo.launch(share=True)