cheberle commited on
Commit
3a12042
·
1 Parent(s): 6564d91
Files changed (2) hide show
  1. app.py +23 -26
  2. requirements.txt +2 -2
app.py CHANGED
@@ -1,32 +1,29 @@
1
- import gradio as gr
2
- from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification
3
 
4
- # Load model and tokenizer
5
- model_name = "cheberle/autotrain-35swc-b4r9z"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
 
8
- # Explicitly define the model configuration if needed
9
- config = AutoConfig.from_pretrained(model_name)
10
- model = AutoModelForSequenceClassification.from_pretrained(model_name, config=config)
 
 
 
 
11
 
12
- # Inference function
13
- def classify_text(input_text):
14
- inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True)
15
- outputs = model(**inputs)
16
- probabilities = outputs.logits.softmax(dim=-1).tolist()[0]
17
- labels = {i: f"Label {i}" for i in range(len(probabilities))} # Define label mapping if needed
18
- result = {labels[i]: prob for i, prob in enumerate(probabilities)}
19
- return result
20
 
21
- # Gradio interface
22
- interface = gr.Interface(
23
- fn=classify_text,
24
- inputs="text",
25
- outputs="label",
26
- title="DeepSeek-R1 Text Classification",
27
- description="Classify text inputs using the DeepSeek-R1 model."
28
  )
29
 
30
- # Launch the app
31
- if __name__ == "__main__":
32
- interface.launch()
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
2
 
3
+ model_path = "cheberle/autotrain-35swc-b4r9z"
 
 
4
 
5
+ # Load the tokenizer and model
6
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
7
+ model = AutoModelForCausalLM.from_pretrained(
8
+ model_path,
9
+ device_map="auto", # Auto-distributes model across available devices
10
+ torch_dtype="auto" # Automatically selects the appropriate data type
11
+ ).eval()
12
 
13
+ # Prompt content
14
+ messages = [{"role": "user", "content": "hi"}]
 
 
 
 
 
 
15
 
16
+ # Prepare input for the model
17
+ input_ids = tokenizer.apply_chat_template(
18
+ conversation=messages,
19
+ tokenize=True,
20
+ add_generation_prompt=True,
21
+ return_tensors='pt'
 
22
  )
23
 
24
+ # Generate response
25
+ output_ids = model.generate(input_ids.to('cuda')) # Ensure the model uses the GPU if available
26
+ response = tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
27
+
28
+ # Print response
29
+ print(response)
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
  huggingface_hub==0.25.2
2
- gradio
3
  torch
4
- transformers
 
1
  huggingface_hub==0.25.2
2
+ transformers
3
  torch
4
+ accelerate