travahacker commited on
Commit
d3d6e15
Β·
1 Parent(s): d05c459

Fix: Add HF token support and error handling for API authentication

Browse files
Files changed (2) hide show
  1. HF_TOKEN_SETUP.md +58 -0
  2. app.py +31 -18
HF_TOKEN_SETUP.md ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # πŸ”‘ How to Add HuggingFace Token to Your Space
2
+
3
+ The AlgoSpeak AI app needs a HuggingFace token to access the Mistral-7B model for better translation quality.
4
+
5
+ ## Why do I need this?
6
+
7
+ - **Without token**: Uses Google Flan-T5 (smaller, limited quality)
8
+ - **With token**: Uses Mistral-7B (better understanding, higher quality)
9
+
10
+ ## Step-by-Step Instructions
11
+
12
+ ### 1. Create a HuggingFace Access Token
13
+
14
+ 1. Go to [HuggingFace Settings β†’ Tokens](https://huggingface.co/settings/tokens)
15
+ 2. Click **"New token"**
16
+ 3. Give it a name like `algospeak-space`
17
+ 4. Select permission: **Read** (sufficient for inference)
18
+ 5. Click **"Generate"**
19
+ 6. **Copy the token** (you won't see it again!)
20
+
21
+ ### 2. Add Token as Secret to Your Space
22
+
23
+ 1. Go to your Space: https://huggingface.co/spaces/Veronyka/algospeak
24
+ 2. Click on **"Settings"** tab (βš™οΈ)
25
+ 3. Scroll to **"Repository secrets"** section
26
+ 4. Click **"New secret"**
27
+ 5. Fill in:
28
+ - **Name:** `HF_TOKEN`
29
+ - **Value:** Paste your token here
30
+ 6. Click **"Add secret"**
31
+
32
+ ### 3. Restart Your Space
33
+
34
+ 1. Go to **"Settings"** β†’ **"Factory reboot"**
35
+ 2. Or just wait ~1 minute for automatic restart
36
+
37
+ ### 4. Verify It's Working
38
+
39
+ - The app header should now show: `πŸ€– Current Model: mistralai/Mistral-7B-Instruct-v0.3`
40
+ - Translations should be more contextual and higher quality
41
+
42
+ ## Troubleshooting
43
+
44
+ ### Still seeing errors?
45
+
46
+ - Check if the secret name is exactly `HF_TOKEN` (case-sensitive)
47
+ - Make sure your token has "Read" permission
48
+ - Try Factory reboot in Settings
49
+
50
+ ### Token not being used?
51
+
52
+ - The app will automatically detect the token via environment variable
53
+ - No code changes needed!
54
+
55
+ ---
56
+
57
+ **Security Note:** Secrets in HF Spaces are encrypted and not visible to visitors. Only the Space owner can see/edit them.
58
+
app.py CHANGED
@@ -1,13 +1,18 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from algospeak_dictionary import get_algospeak_context, ALGOSPEAK_DICT
 
4
 
5
- # Initialize inference client
6
- client = InferenceClient()
 
7
 
8
  # Complete AlgoSpeak dictionary for prompts
9
  ALGOSPEAK_EXAMPLES = get_algospeak_context()
10
 
 
 
 
11
  def translate_to_algospeak(text):
12
  """Translates normal text to AlgoSpeak"""
13
  prompt = f"""You are an expert in AlgoSpeak - coded language used to circumvent algorithmic moderation on social media.
@@ -24,13 +29,16 @@ Original text: {text}
24
 
25
  Translation to AlgoSpeak:"""
26
 
27
- response = client.text_generation(
28
- prompt,
29
- model="mistralai/Mistral-7B-Instruct-v0.3",
30
- max_new_tokens=250,
31
- temperature=0.7
32
- )
33
- return response
 
 
 
34
 
35
  def interpret_algospeak(text):
36
  """Interprets AlgoSpeak to plain language"""
@@ -48,13 +56,16 @@ AlgoSpeak text: {text}
48
 
49
  Interpretation in plain language:"""
50
 
51
- response = client.text_generation(
52
- prompt,
53
- model="mistralai/Mistral-7B-Instruct-v0.3",
54
- max_new_tokens=250,
55
- temperature=0.3
56
- )
57
- return response
 
 
 
58
 
59
  def search_dictionary(query):
60
  """Search terms in the AlgoSpeak dictionary"""
@@ -72,7 +83,7 @@ def search_dictionary(query):
72
 
73
  # Interface Gradio
74
  with gr.Blocks(theme=gr.themes.Soft(), title="AlgoSpeak AI") as demo:
75
- gr.Markdown("""
76
  # πŸ—£οΈ AlgoSpeak AI
77
 
78
  **AlgoSpeak** is a language used to circumvent content moderation algorithms on social media platforms.
@@ -80,7 +91,9 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AlgoSpeak AI") as demo:
80
  This AI can:
81
  - πŸ“ Translate plain text β†’ AlgoSpeak
82
  - πŸ” Interpret AlgoSpeak β†’ plain language
83
- - πŸ“– Search through 50+ catalogued terms
 
 
84
  """)
85
 
86
  with gr.Tab("🌐 Translate to AlgoSpeak"):
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from algospeak_dictionary import get_algospeak_context, ALGOSPEAK_DICT
4
+ import os
5
 
6
+ # Initialize inference client with HF token (automatically provided in Spaces)
7
+ hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_TOKEN")
8
+ client = InferenceClient(token=hf_token)
9
 
10
  # Complete AlgoSpeak dictionary for prompts
11
  ALGOSPEAK_EXAMPLES = get_algospeak_context()
12
 
13
+ # Model selection based on token availability
14
+ MODEL = "mistralai/Mistral-7B-Instruct-v0.3" if hf_token else "google/flan-t5-large"
15
+
16
  def translate_to_algospeak(text):
17
  """Translates normal text to AlgoSpeak"""
18
  prompt = f"""You are an expert in AlgoSpeak - coded language used to circumvent algorithmic moderation on social media.
 
29
 
30
  Translation to AlgoSpeak:"""
31
 
32
+ try:
33
+ response = client.text_generation(
34
+ prompt,
35
+ model=MODEL,
36
+ max_new_tokens=250,
37
+ temperature=0.7
38
+ )
39
+ return response
40
+ except Exception as e:
41
+ return f"⚠️ Error: {str(e)}\n\nTip: The Space may need a HuggingFace token. Contact the Space owner."
42
 
43
  def interpret_algospeak(text):
44
  """Interprets AlgoSpeak to plain language"""
 
56
 
57
  Interpretation in plain language:"""
58
 
59
+ try:
60
+ response = client.text_generation(
61
+ prompt,
62
+ model=MODEL,
63
+ max_new_tokens=250,
64
+ temperature=0.3
65
+ )
66
+ return response
67
+ except Exception as e:
68
+ return f"⚠️ Error: {str(e)}\n\nTip: The Space may need a HuggingFace token. Contact the Space owner."
69
 
70
  def search_dictionary(query):
71
  """Search terms in the AlgoSpeak dictionary"""
 
83
 
84
  # Interface Gradio
85
  with gr.Blocks(theme=gr.themes.Soft(), title="AlgoSpeak AI") as demo:
86
+ gr.Markdown(f"""
87
  # πŸ—£οΈ AlgoSpeak AI
88
 
89
  **AlgoSpeak** is a language used to circumvent content moderation algorithms on social media platforms.
 
91
  This AI can:
92
  - πŸ“ Translate plain text β†’ AlgoSpeak
93
  - πŸ” Interpret AlgoSpeak β†’ plain language
94
+ - πŸ“– Search through 60+ catalogued terms
95
+
96
+ πŸ€– **Current Model:** `{MODEL}`
97
  """)
98
 
99
  with gr.Tab("🌐 Translate to AlgoSpeak"):