DevStudio-AI commited on
Commit
d4bd786
·
verified ·
1 Parent(s): 03d70e4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import os
3
+ import gradio as gr
4
+ import torch
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
+ import spaces # Mandatory library for Hugging Face ZeroGPU [1]
7
+
8
+ MODEL_ID = "DevStudio-AI/Devstudio-Coder-1.5B"
9
+
10
+ print("Loading tokenizer and base model...")
11
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
12
+
13
+ # We load the model in 16-bit on CPU first
14
+ # ZeroGPU will automatically move the model to the GPU when the decorated function runs [1]
15
+ model = AutoModelForCausalLM.from_pretrained(
16
+ MODEL_ID,
17
+ torch_dtype=torch.float16,
18
+ device_map="cpu"
19
+ )
20
+ print("Model successfully loaded on CPU. Awaiting ZeroGPU allocation...")
21
+
22
+ # The @spaces.GPU decorator dynamically requests Nvidia A100 resources for this call [1]
23
+ @spaces.GPU
24
+ def generate_code(prompt, temperature, max_tokens):
25
+ try:
26
+ # Move model to CUDA dynamically inside the GPU context [1]
27
+ model.to("cuda")
28
+
29
+ inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
30
+
31
+ outputs = model.generate(
32
+ **inputs,
33
+ max_new_tokens=int(max_tokens),
34
+ temperature=float(temperature),
35
+ do_sample=True,
36
+ eos_token_id=tokenizer.eos_token_id
37
+ )
38
+
39
+ # Isolate and decode newly generated tokens
40
+ generated_ids = outputs[0][inputs["input_ids"].shape[1]:]
41
+ return tokenizer.decode(generated_ids, skip_special_tokens=True)
42
+ except Exception as e:
43
+ return f"Error during generation: {str(e)}"
44
+
45
+ # Define the Gradio web interface
46
+ demo = gr.Interface(
47
+ fn=generate_code,
48
+ inputs=[
49
+ gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
50
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.3, label="Temperature"),
51
+ gr.Slider(minimum=64, maximum=2048, value=1024, step=64, label="Max Tokens")
52
+ ],
53
+ outputs=gr.Textbox(label="Generated Code"),
54
+ title="DevStudio-1.5B API Engine",
55
+ description="Static HTML + Tailwind CSS specialized completion endpoint running on ZeroGPU."
56
+ )
57
+
58
+ demo.launch()