pentarosarium commited on
Commit
fe6b622
·
1 Parent(s): 225995d

progress more 55

Browse files
Files changed (2) hide show
  1. app.py +46 -2
  2. requirements.txt +2 -1
app.py CHANGED
@@ -20,6 +20,10 @@ from langchain.prompts import PromptTemplate
20
  from langchain.chains import LLMChain
21
  from huggingface_hub import login
22
  from accelerate import init_empty_weights
 
 
 
 
23
 
24
 
25
  # Initialize pymystem3 for lemmatization
@@ -33,7 +37,45 @@ finbert_tone = pipeline("sentiment-analysis", model="yiyanghkust/finbert-tone")
33
  rubert1 = pipeline("sentiment-analysis", model = "DeepPavlov/rubert-base-cased")
34
  rubert2 = pipeline("sentiment-analysis", model = "blanchefort/rubert-base-cased-sentiment")
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def init_langchain_llm():
 
 
 
 
 
 
 
 
37
  # Authenticate using the token from Streamlit secrets
38
  if 'hf_token' in st.secrets:
39
  login(token=st.secrets['hf_token'])
@@ -72,8 +114,10 @@ def init_langchain_llm():
72
 
73
  llm = HuggingFacePipeline(pipeline=llama_wrapper)
74
  return llm
 
75
  except Exception as e:
76
- st.error(f"Error initializing the model: {str(e)}")
 
77
  st.stop()
78
 
79
  def estimate_impact(llm, news_text, entity):
@@ -448,7 +492,7 @@ def create_output_file(df, uploaded_file, analysis_df):
448
  return output
449
 
450
  def main():
451
- st.title("... приступим к анализу... версия 54")
452
 
453
  # Initialize session state
454
  if 'processed_df' not in st.session_state:
 
20
  from langchain.chains import LLMChain
21
  from huggingface_hub import login
22
  from accelerate import init_empty_weights
23
+ import logging
24
+
25
+ logging.basicConfig(level=logging.INFO)
26
+ logger = logging.getLogger(__name__)
27
 
28
 
29
  # Initialize pymystem3 for lemmatization
 
37
  rubert1 = pipeline("sentiment-analysis", model = "DeepPavlov/rubert-base-cased")
38
  rubert2 = pipeline("sentiment-analysis", model = "blanchefort/rubert-base-cased-sentiment")
39
 
40
+
41
+
42
+ def authenticate_huggingface():
43
+ # Try to get the token from environment variable first
44
+ hf_token = os.environ.get('HF_TOKEN')
45
+
46
+ # If not in environment, try Streamlit secrets
47
+ if not hf_token and 'hf_token' in st.secrets:
48
+ hf_token = st.secrets['hf_token']
49
+
50
+ if hf_token:
51
+ login(token=hf_token)
52
+ return True
53
+ else:
54
+ st.error("Hugging Face token not found. Please set HF_TOKEN environment variable or add it to Streamlit secrets.")
55
+ return False
56
+
57
+ @st.cache_resource
58
+ def load_model(model_id):
59
+ tokenizer = transformers.AutoTokenizer.from_pretrained(model_id)
60
+ model = transformers.AutoModelForCausalLM.from_pretrained(
61
+ model_id,
62
+ torch_dtype=torch.float16,
63
+ device_map="cpu",
64
+ low_cpu_mem_usage=True
65
+ )
66
+ return tokenizer, model
67
+
68
+
69
+
70
  def init_langchain_llm():
71
+
72
+ if not authenticate_huggingface():
73
+ st.stop()
74
+
75
+ try:
76
+ model_id = "meta-llama/Meta-Llama-3.1-8B-Instruct"
77
+ tokenizer, model = load_model(model_id)
78
+
79
  # Authenticate using the token from Streamlit secrets
80
  if 'hf_token' in st.secrets:
81
  login(token=st.secrets['hf_token'])
 
114
 
115
  llm = HuggingFacePipeline(pipeline=llama_wrapper)
116
  return llm
117
+
118
  except Exception as e:
119
+ logger.error(f"Error loading model: {str(e)}", exc_info=True)
120
+ st.error(f"Failed to load model: {str(e)}")
121
  st.stop()
122
 
123
  def estimate_impact(llm, news_text, entity):
 
492
  return output
493
 
494
  def main():
495
+ st.title("... приступим к анализу... версия 55")
496
 
497
  # Initialize session state
498
  if 'processed_df' not in st.session_state:
requirements.txt CHANGED
@@ -13,4 +13,5 @@ sacremoses
13
  langchain
14
  langchain-community
15
  huggingface_hub
16
- accelerate>=0.26.0
 
 
13
  langchain
14
  langchain-community
15
  huggingface_hub
16
+ accelerate>=0.26.0
17
+ logging