oscarwang2 commited on
Commit
3176bee
·
verified ·
1 Parent(s): b8b3502

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -4
app.py CHANGED
@@ -1,16 +1,38 @@
 
 
1
  from flask import Flask, request, jsonify
2
  import requests
3
- import torch
4
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
 
 
 
 
 
6
  app = Flask(__name__)
7
 
8
  # Define the SearXNG instance URL
9
  SEARXNG_INSTANCE_URL = "https://oscarwang2-searxng.hf.space/search"
10
 
11
- # Load the educational content classifier
12
- tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/fineweb-edu-classifier")
13
- model = AutoModelForSequenceClassification.from_pretrained("HuggingFaceTB/fineweb-edu-classifier")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  def classify_educational_quality(text):
16
  """
 
1
+ import os
2
+ import torch
3
  from flask import Flask, request, jsonify
4
  import requests
 
5
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
6
 
7
+ # Set cache directory explicitly
8
+ os.environ['TRANSFORMERS_CACHE'] = '/tmp/huggingface_cache'
9
+ os.makedirs('/tmp/huggingface_cache', exist_ok=True)
10
+
11
  app = Flask(__name__)
12
 
13
  # Define the SearXNG instance URL
14
  SEARXNG_INSTANCE_URL = "https://oscarwang2-searxng.hf.space/search"
15
 
16
+ # Load the educational content classifier with explicit cache directory
17
+ def load_model_with_retry(max_retries=3):
18
+ for attempt in range(max_retries):
19
+ try:
20
+ tokenizer = AutoTokenizer.from_pretrained(
21
+ "HuggingFaceTB/fineweb-edu-classifier",
22
+ cache_dir='/tmp/huggingface_cache'
23
+ )
24
+ model = AutoModelForSequenceClassification.from_pretrained(
25
+ "HuggingFaceTB/fineweb-edu-classifier",
26
+ cache_dir='/tmp/huggingface_cache'
27
+ )
28
+ return tokenizer, model
29
+ except Exception as e:
30
+ print(f"Model loading attempt {attempt + 1} failed: {e}")
31
+ if attempt == max_retries - 1:
32
+ raise
33
+
34
+ # Load models at startup
35
+ tokenizer, model = load_model_with_retry()
36
 
37
  def classify_educational_quality(text):
38
  """