arithescientist commited on
Commit
32ba6b8
·
verified ·
1 Parent(s): 110bab7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -5
app.py CHANGED
@@ -7,6 +7,7 @@ import sqlparse
7
  import logging
8
 
9
  # Import necessary modules from transformers and langchain
 
10
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
11
  from langchain.llms import HuggingFacePipeline
12
 
@@ -24,14 +25,20 @@ if hf_token is None:
24
  st.error("Hugging Face API token is not set. Please set the HUGGINGFACEHUB_API_TOKEN secret in your Space.")
25
  st.stop()
26
 
 
 
 
 
 
 
27
  # Load the tokenizer and model with the token
28
  tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=hf_token)
29
  model = AutoModelForCausalLM.from_pretrained(
30
  model_id,
31
  use_auth_token=hf_token,
32
- device_map='auto',
33
- torch_dtype='auto' # Adjust based on your environment
34
- )
35
 
36
  # Create the text-generation pipeline with appropriate parameters
37
  pipe = pipeline(
@@ -43,13 +50,13 @@ pipe = pipeline(
43
  repetition_penalty=1.1,
44
  do_sample=True, # Use sampling to introduce some randomness
45
  eos_token_id=tokenizer.eos_token_id,
46
- pad_token_id=tokenizer.eos_token_id
 
47
  )
48
 
49
  # Wrap the pipeline with HuggingFacePipeline for use in LangChain
50
  llm = HuggingFacePipeline(pipeline=pipe)
51
 
52
- # ... rest of your code ...
53
 
54
 
55
  # Step 1: Upload CSV data file (or use default)
 
7
  import logging
8
 
9
  # Import necessary modules from transformers and langchain
10
+ import torch
11
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
12
  from langchain.llms import HuggingFacePipeline
13
 
 
25
  st.error("Hugging Face API token is not set. Please set the HUGGINGFACEHUB_API_TOKEN secret in your Space.")
26
  st.stop()
27
 
28
+ # Import torch
29
+ import torch
30
+
31
+ # Set device
32
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
33
+
34
  # Load the tokenizer and model with the token
35
  tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=hf_token)
36
  model = AutoModelForCausalLM.from_pretrained(
37
  model_id,
38
  use_auth_token=hf_token,
39
+ device_map=None, # We'll set the device manually
40
+ torch_dtype=torch.float32 # Use float32 to avoid half-precision issues
41
+ ).to(device)
42
 
43
  # Create the text-generation pipeline with appropriate parameters
44
  pipe = pipeline(
 
50
  repetition_penalty=1.1,
51
  do_sample=True, # Use sampling to introduce some randomness
52
  eos_token_id=tokenizer.eos_token_id,
53
+ pad_token_id=tokenizer.eos_token_id,
54
+ device=0 if torch.cuda.is_available() else -1 # Use GPU if available
55
  )
56
 
57
  # Wrap the pipeline with HuggingFacePipeline for use in LangChain
58
  llm = HuggingFacePipeline(pipeline=pipe)
59
 
 
60
 
61
 
62
  # Step 1: Upload CSV data file (or use default)