jeyanthangj2004 commited on
Commit
f130a02
·
verified ·
1 Parent(s): 2c20d66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -37
app.py CHANGED
@@ -1,57 +1,71 @@
 
1
  import gradio as gr
2
- from llama_cpp import Llama
3
- from huggingface_hub import hf_hub_download
4
-
5
- # =========================
6
- # MODEL AUTO-DOWNLOAD
7
- # =========================
8
- MODEL_PATH = hf_hub_download(
9
- repo_id="Qwen/Qwen2.5-1.5B-Instruct-GGUF",
10
- filename="qwen2.5-1.5b-instruct-q4_k_m.gguf"
 
11
  )
12
 
13
- # =========================
14
- # LOAD MODEL (CPU OPTIMIZED)
15
- # =========================
16
- llm = Llama(
17
- model_path=MODEL_PATH,
18
- n_ctx=4096, # RAG-friendly
19
- n_threads=2, # HF free CPU
20
- n_batch=256,
21
- verbose=False
22
  )
23
 
24
- # =========================
25
- # GENERATION FUNCTION
26
- # =========================
 
 
27
  def generate(
28
  prompt,
29
- max_new_tokens=1024,
30
- temperature=0.2,
31
  top_p=0.9
32
  ):
33
- output = llm(
34
  prompt,
35
- max_tokens=max_new_tokens,
36
- temperature=temperature,
37
- top_p=top_p,
38
- stop=["<|endoftext|>"]
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  )
40
- return output["choices"][0]["text"]
41
 
42
- # =========================
43
- # GRADIO INTERFACE (API ENABLED)
44
- # =========================
45
  demo = gr.Interface(
46
  fn=generate,
47
  inputs=[
48
- gr.Textbox(label="Prompt / Context", lines=10),
49
- gr.Slider(256, 2048, value=1024, step=128, label="Max New Tokens"),
50
- gr.Slider(0.1, 1.0, value=0.2, step=0.05, label="Temperature"),
51
  gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
52
  ],
53
- outputs=gr.Textbox(label="Response", lines=15),
54
- title="Qwen2.5-1.5B-Instruct (GGUF Q4 • FAST CPU • No Rate Limits)",
55
  )
56
 
57
  demo.launch()
 
1
+ import torch
2
  import gradio as gr
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+
5
+ MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
6
+
7
+ # -----------------------------
8
+ # Load tokenizer
9
+ # -----------------------------
10
+ tokenizer = AutoTokenizer.from_pretrained(
11
+ MODEL_ID,
12
+ use_fast=True
13
  )
14
 
15
+ # -----------------------------
16
+ # Load model (CPU, non-quantized)
17
+ # -----------------------------
18
+ model = AutoModelForCausalLM.from_pretrained(
19
+ MODEL_ID,
20
+ torch_dtype=torch.float32,
21
+ device_map="cpu"
 
 
22
  )
23
 
24
+ model.eval()
25
+
26
+ # -----------------------------
27
+ # Generation function
28
+ # -----------------------------
29
  def generate(
30
  prompt,
31
+ max_new_tokens=512,
32
+ temperature=0.7,
33
  top_p=0.9
34
  ):
35
+ inputs = tokenizer(
36
  prompt,
37
+ return_tensors="pt",
38
+ truncation=True,
39
+ max_length=2048
40
+ )
41
+
42
+ with torch.no_grad():
43
+ outputs = model.generate(
44
+ **inputs,
45
+ max_new_tokens=max_new_tokens,
46
+ temperature=temperature,
47
+ top_p=top_p,
48
+ do_sample=True
49
+ )
50
+
51
+ return tokenizer.decode(
52
+ outputs[0],
53
+ skip_special_tokens=True
54
  )
 
55
 
56
+ # -----------------------------
57
+ # Gradio Interface (API enabled)
58
+ # -----------------------------
59
  demo = gr.Interface(
60
  fn=generate,
61
  inputs=[
62
+ gr.Textbox(label="Prompt", lines=6),
63
+ gr.Slider(64, 1024, value=512, step=64, label="Max New Tokens"),
64
+ gr.Slider(0.1, 1.0, value=0.7, step=0.05, label="Temperature"),
65
  gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
66
  ],
67
+ outputs=gr.Textbox(label="Response", lines=10),
68
+ title="TinyLlama-1.1B-Chat (Non-Quantized, CPU)"
69
  )
70
 
71
  demo.launch()