Arko007 commited on
Commit
d470a4d
·
verified ·
1 Parent(s): 3c7995c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -16
app.py CHANGED
@@ -1,11 +1,11 @@
1
  # ==============================================================================
2
- # V5 GRADIO DEPLOYMENT SCRIPT
3
  # ==============================================================================
4
- # This script creates a web UI to test your v5 model.
5
  #
6
  # TO DEPLOY ON HUGGING FACE SPACES:
7
  # 1. Create a new Space and choose the "Gradio" SDK.
8
- # 2. Select the free "T4 small" GPU hardware.
9
  # 3. Create a file named `app.py` and paste this code into it.
10
  # 4. Create a `requirements.txt` file and add the libraries listed below.
11
  # ==============================================================================
@@ -48,44 +48,134 @@ tokenizer.pad_token = tokenizer.eos_token
48
  print("✅ Model loaded successfully!")
49
 
50
  # --- Inference Function ---
51
- def generate_code(instruction):
52
  """
53
  Generates code from an instruction using the v5 model.
54
  """
 
55
  prompt = f"""### Instruction:
56
  {instruction}
57
 
58
  ### Response:"""
59
 
60
- inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
 
61
 
 
62
  outputs = model.generate(
63
  **inputs,
64
- max_new_tokens=2048, # Increased token limit for longer code generation
65
  temperature=0.1,
66
  top_p=0.9,
67
  eos_token_id=tokenizer.eos_token_id
68
  )
69
 
 
70
  response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
71
- # Extract only the code part of the response
72
  code_part = response_text.split("### Response:")[1].strip()
73
  return code_part
74
 
75
  # --- Create and Launch the Gradio Web App ---
76
- print("Launching Gradio app...")
77
 
78
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  gr.Markdown("# 🤖 My Awesome Code Assistant (v5)")
80
- gr.Markdown("Enter an instruction and I'll generate the code for you!")
81
 
82
- with gr.Row():
83
- instruction_box = gr.Textbox(lines=5, label="Instruction", placeholder="e.g., Write a Python function to sort a list of numbers.")
84
- output_box = gr.Code(label="Generated Code", language="python")
 
 
 
 
 
85
 
86
- generate_button = gr.Button("Generate Code", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
- generate_button.click(fn=generate_code, inputs=instruction_box, outputs=output_box)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  # This will launch the app when deployed on Hugging Face Spaces
91
- demo.launch(share=True)
 
1
  # ==============================================================================
2
+ # V5 GRADIO DEPLOYMENT SCRIPT (UPGRADED UI)
3
  # ==============================================================================
4
+ # This script creates a beautiful, user-friendly web UI for your v5 model.
5
  #
6
  # TO DEPLOY ON HUGGING FACE SPACES:
7
  # 1. Create a new Space and choose the "Gradio" SDK.
8
+ # 2. Select the free "CPU basic" hardware.
9
  # 3. Create a file named `app.py` and paste this code into it.
10
  # 4. Create a `requirements.txt` file and add the libraries listed below.
11
  # ==============================================================================
 
48
  print("✅ Model loaded successfully!")
49
 
50
  # --- Inference Function ---
51
+ def generate_code(instruction, progress=gr.Progress(track_tqdm=True)):
52
  """
53
  Generates code from an instruction using the v5 model.
54
  """
55
+ progress(0, desc="Formatting prompt...")
56
  prompt = f"""### Instruction:
57
  {instruction}
58
 
59
  ### Response:"""
60
 
61
+ # *** FIX: Use model.device to automatically select CPU or GPU ***
62
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
63
 
64
+ progress(0.2, desc="Generating tokens...")
65
  outputs = model.generate(
66
  **inputs,
67
+ max_new_tokens=2048,
68
  temperature=0.1,
69
  top_p=0.9,
70
  eos_token_id=tokenizer.eos_token_id
71
  )
72
 
73
+ progress(0.8, desc="Decoding response...")
74
  response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
75
  code_part = response_text.split("### Response:")[1].strip()
76
  return code_part
77
 
78
  # --- Create and Launch the Gradio Web App ---
79
+ print("Launching Gradio app with upgraded UI...")
80
 
81
+ # *** FIX: Add custom CSS and a better theme ***
82
+ css = """
83
+ body { background-color: #0F172A; }
84
+ .gradio-container { max-width: 800px !important; margin: auto !important; }
85
+ footer { display: none !important; }
86
+ """
87
+
88
+ # *** FIX: Use a modern theme ***
89
+ theme = gr.themes.Glass(
90
+ primary_hue="sky",
91
+ secondary_hue="blue",
92
+ neutral_hue="slate"
93
+ ).set(
94
+ body_background_fill="#0F172A",
95
+ block_background_fill="#1E293B",
96
+ block_border_width="1px",
97
+ block_title_background_fill="none",
98
+ input_background_fill="#0F172A",
99
+ )
100
+
101
+ with gr.Blocks(theme=theme, css=css) as demo:
102
  gr.Markdown("# 🤖 My Awesome Code Assistant (v5)")
103
+ gr.Markdown("### Built by Arko007. Powered by a custom fine-tuned Code Llama model.")
104
 
105
+ with gr.Column():
106
+ instruction_box = gr.Textbox(
107
+ lines=5,
108
+ label="Instruction",
109
+ placeholder="e.g., Write a Python script to scrape a website for all its links.",
110
+ elem_id="instruction-textbox" # Add an ID for our JavaScript
111
+ )
112
+ output_box = gr.Code(label="Generated Code", language="python", interactive=False)
113
 
114
+ generate_button = gr.Button("Generate Code", variant="primary", elem_id="generate-button")
115
+
116
+ gr.Markdown("---")
117
+ gr.Markdown("### Or, try one of these examples:")
118
+ examples = gr.Examples(
119
+ examples=[
120
+ "Write a Python function to find the factorial of a number using recursion.",
121
+ "Create a simple Flask API with a single endpoint that returns 'Hello, World!'.",
122
+ "Write a C++ program to implement a binary search tree.",
123
+ "Explain the concept of closures in JavaScript with a code example."
124
+ ],
125
+ inputs=instruction_box
126
+ )
127
 
128
+ # Connect the button click to the function
129
+ generate_button.click(
130
+ fn=generate_code,
131
+ inputs=instruction_box,
132
+ outputs=output_box
133
+ )
134
+
135
+ # *** FIX: Add JavaScript to make "Enter" submit the form ***
136
+ # This JS listens for a keypress on our textbox. If the key is Enter
137
+ # and the Shift key is NOT held down, it clicks the generate button.
138
+ js_code = """
139
+ <script>
140
+ function onKeyPress(event) {
141
+ var instructionTextbox = document.getElementById('instruction-textbox').querySelector('textarea');
142
+ if (event.key === 'Enter' && !event.shiftKey) {
143
+ event.preventDefault(); // Prevent new line
144
+ var generateButton = document.getElementById('generate-button');
145
+ generateButton.click(); // Click the button
146
+ }
147
+ }
148
+ // We need to wait for the Gradio app to mount the elements
149
+ document.addEventListener('DOMContentLoaded', function() {
150
+ var instructionTextbox = document.getElementById('instruction-textbox');
151
+ if (instructionTextbox) {
152
+ var textarea = instructionTextbox.querySelector('textarea');
153
+ if (textarea) {
154
+ textarea.addEventListener('keydown', onKeyPress);
155
+ }
156
+ }
157
+ });
158
+
159
+ // Gradio can be slow to load, so we'll also use a MutationObserver
160
+ // to make sure we attach the event listener even if the element appears later.
161
+ const observer = new MutationObserver((mutations, obs) => {
162
+ const instructionTextbox = document.getElementById('instruction-textbox');
163
+ if (instructionTextbox) {
164
+ const textarea = instructionTextbox.querySelector('textarea');
165
+ if (textarea) {
166
+ textarea.addEventListener('keydown', onKeyPress);
167
+ obs.disconnect(); // Stop observing once we've found it
168
+ }
169
+ }
170
+ });
171
+ observer.observe(document.body, {
172
+ childList: true,
173
+ subtree: true
174
+ });
175
+ </script>
176
+ """
177
+ gr.HTML(js_code)
178
+
179
 
180
  # This will launch the app when deployed on Hugging Face Spaces
181
+ demo.launch()