cheberle commited on
Commit
4460d3d
·
1 Parent(s): 85ca4b3
Files changed (2) hide show
  1. app.py +80 -22
  2. requirements.txt +2 -3
app.py CHANGED
@@ -1,27 +1,85 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModel
 
3
 
4
- # Load the model and tokenizer with `trust_remote_code=True`
5
- model_name = "deepseek-ai/DeepSeek-R1"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
7
- model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- def classify_text(input_text):
10
- # Tokenize input
11
- inputs = tokenizer(input_text, return_tensors="pt")
12
- # Get model output
13
- outputs = model(**inputs)
14
- probabilities = outputs.logits.softmax(dim=-1).detach().numpy()
15
- return {f"Class {i}": prob for i, prob in enumerate(probabilities[0])}
16
-
17
- # Create Gradio interface
18
- interface = gr.Interface(
19
- fn=classify_text,
20
- inputs=gr.Textbox(label="Enter Text"),
21
- outputs=gr.Label(label="Class Probabilities"),
22
- title="DeepSeek-R1 Text Classification",
23
- description="A text classification app powered by DeepSeek-R1."
24
  )
25
 
26
- # Launch the app
27
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
5
+ # ---------------------------------------------------------------------------
6
+ # 1) Load the model and tokenizer
7
+ # ---------------------------------------------------------------------------
8
+ # If you want to load in 8-bit or 4-bit precision with bitsandbytes,
9
+ # uncomment and install bitsandbytes, and set load_in_8bit=True or load_in_4bit=True.
10
+ # For example:
11
+ #
12
+ # from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
13
+ # bnb_config = BitsAndBytesConfig(
14
+ # load_in_4bit=True, # or load_in_8bit=True
15
+ # bnb_4bit_compute_dtype=torch.float16, # recommended compute dtype
16
+ # bnb_4bit_use_double_quant=True, # optional
17
+ # bnb_4bit_quant_type='nf4', # optional
18
+ # )
19
+ #
20
+ # model = AutoModelForCausalLM.from_pretrained(
21
+ # "cheberle/autotrain-35swc-b4r9z",
22
+ # quantization_config=bnb_config,
23
+ # device_map="auto",
24
+ # trust_remote_code=True
25
+ # )
26
+ # tokenizer = AutoTokenizer.from_pretrained("cheberle/autotrain-35swc-b4r9z", trust_remote_code=True)
27
 
28
+ # For a standard FP16 or FP32 load (no bitsandbytes):
29
+ model = AutoModelForCausalLM.from_pretrained(
30
+ "cheberle/autotrain-35swc-b4r9z",
31
+ device_map="auto", # Device automatically mapped across GPUs or CPU
32
+ torch_dtype=torch.float16, # Or "auto", or float32
33
+ trust_remote_code=True
34
+ )
35
+ tokenizer = AutoTokenizer.from_pretrained(
36
+ "cheberle/autotrain-35swc-b4r9z",
37
+ trust_remote_code=True
 
 
 
 
 
38
  )
39
 
40
+ # ---------------------------------------------------------------------------
41
+ # 2) Define a text generation function
42
+ # ---------------------------------------------------------------------------
43
+ def generate_text(prompt):
44
+ # Tokenize input
45
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
46
+
47
+ # Generate output (configure generation args as needed)
48
+ with torch.no_grad():
49
+ outputs = model.generate(
50
+ **inputs,
51
+ max_new_tokens=128,
52
+ temperature=0.7,
53
+ top_p=0.9,
54
+ do_sample=True
55
+ )
56
+
57
+ # Decode
58
+ decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
59
+ return decoded
60
+
61
+ # ---------------------------------------------------------------------------
62
+ # 3) Create the Gradio interface
63
+ # ---------------------------------------------------------------------------
64
+ with gr.Blocks() as demo:
65
+ gr.Markdown("<h3>Demo: cheberle/autotrain-35swc-b4r9z</h3>")
66
+
67
+ with gr.Row():
68
+ with gr.Column():
69
+ prompt_in = gr.Textbox(
70
+ lines=5,
71
+ label="Enter your prompt",
72
+ placeholder="Ask something here..."
73
+ )
74
+ submit_btn = gr.Button("Generate")
75
+ with gr.Column():
76
+ output_box = gr.Textbox(lines=15, label="Model Output")
77
+
78
+ # Define what happens on button click
79
+ submit_btn.click(fn=generate_text, inputs=prompt_in, outputs=output_box)
80
+
81
+ # ---------------------------------------------------------------------------
82
+ # 4) Launch!
83
+ # ---------------------------------------------------------------------------
84
+ if __name__ == "__main__":
85
+ demo.launch()
requirements.txt CHANGED
@@ -1,5 +1,4 @@
1
  huggingface_hub==0.25.2
2
- transformers
3
  torch
4
- datasets
5
- scipy
 
1
  huggingface_hub==0.25.2
 
2
  torch
3
+ transformers
4
+ gradio