coder5809 commited on
Commit
92fad90
·
verified ·
1 Parent(s): cad67c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -3,8 +3,9 @@ import torch
3
  import spaces
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
 
6
- # Load model & tokenizer outside the decorated function to keep them in RAM
7
- model_id = "Qwen/Qwen2.5-1.5B-Instruct"
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_id)
9
  model = AutoModelForCausalLM.from_pretrained(
10
  model_id,
@@ -12,12 +13,15 @@ model = AutoModelForCausalLM.from_pretrained(
12
  device_map="auto"
13
  )
14
 
15
- # The @spaces.GPU decorator requests temporary GPU access only when a user sends a prompt
16
  @spaces.GPU
17
- def generate(prompt, max_new_tokens=512, temperature=0.7):
 
18
  messages = [
19
- {"role": "system", "content": "You are a helpful assistant."},
20
- {"role": "user", "content": prompt}
 
 
 
21
  ]
22
 
23
  text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
@@ -26,7 +30,7 @@ def generate(prompt, max_new_tokens=512, temperature=0.7):
26
  generated_ids = model.generate(
27
  **model_inputs,
28
  max_new_tokens=max_new_tokens,
29
- temperature=temperature
30
  )
31
 
32
  generated_ids = [
@@ -35,16 +39,15 @@ def generate(prompt, max_new_tokens=512, temperature=0.7):
35
 
36
  return tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
37
 
38
- # Build the Gradio Web Interface
39
  demo = gr.Interface(
40
- fn=generate,
41
  inputs=[
42
- gr.Textbox(lines=4, placeholder="Enter your prompt here..."),
43
  gr.Slider(64, 1024, value=512, label="Max New Tokens"),
44
- gr.Slider(0.1, 1.0, value=0.7, label="Temperature")
45
  ],
46
  outputs="text",
47
- title="Free ZeroGPU LLM Playground"
48
  )
49
 
50
  if __name__ == "__main__":
 
3
  import spaces
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
 
6
+ # Swap in a model with strong JSON generation capabilities
7
+ model_id = "Qwen/Qwen2.5-3B-Instruct"
8
+
9
  tokenizer = AutoTokenizer.from_pretrained(model_id)
10
  model = AutoModelForCausalLM.from_pretrained(
11
  model_id,
 
13
  device_map="auto"
14
  )
15
 
 
16
  @spaces.GPU
17
+ def generate_json(user_prompt, max_new_tokens=512, temperature=0.2):
18
+ # Strict system prompt instructing the model to output raw JSON only
19
  messages = [
20
+ {
21
+ "role": "system",
22
+ "content": "You are a JSON generator. You MUST respond strictly with valid JSON. Do not include markdown code block backticks (like ```json), commentary, or extra text."
23
+ },
24
+ {"role": "user", "content": user_prompt}
25
  ]
26
 
27
  text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
 
30
  generated_ids = model.generate(
31
  **model_inputs,
32
  max_new_tokens=max_new_tokens,
33
+ temperature=temperature # Lower temperature (0.1–0.3) keeps JSON syntax consistent
34
  )
35
 
36
  generated_ids = [
 
39
 
40
  return tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
41
 
 
42
  demo = gr.Interface(
43
+ fn=generate_json,
44
  inputs=[
45
+ gr.Textbox(lines=4, placeholder="e.g., Extract user info: John Doe is 30 years old and lives in NYC."),
46
  gr.Slider(64, 1024, value=512, label="Max New Tokens"),
47
+ gr.Slider(0.0, 1.0, value=0.2, label="Temperature (Keep low for JSON)")
48
  ],
49
  outputs="text",
50
+ title="JSON Output LLM Playground"
51
  )
52
 
53
  if __name__ == "__main__":