sushantvyz commited on
Commit
e9fb849
·
verified ·
1 Parent(s): 4baaa8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -1,9 +1,13 @@
1
  import gradio as gr
2
  import os
 
3
 
4
  # Try multiple ways to get the token
5
  HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HF_API_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
6
 
 
 
 
7
  def generate_output(project_name, user_input, file_path=None):
8
  """Generates specialized output using the LLM based on the project context."""
9
 
@@ -51,13 +55,13 @@ def generate_output(project_name, user_input, file_path=None):
51
 
52
  # Call the HF Inference API using requests
53
  try:
54
- import requests
55
-
56
  API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
57
 
58
- headers = {}
59
  if HF_TOKEN:
60
  headers["Authorization"] = f"Bearer {HF_TOKEN}"
 
 
61
 
62
  # Format the prompt for Mistral
63
  prompt = f"<s>[INST] {system_prompt}\n\n{user_input} [/INST]"
@@ -78,18 +82,18 @@ def generate_output(project_name, user_input, file_path=None):
78
  result = response.json()
79
  if isinstance(result, list) and len(result) > 0:
80
  generated_text = result[0].get('generated_text', '').strip()
81
- return generated_text
82
  else:
83
- return "Error: Unexpected response format from API"
84
  elif response.status_code == 503:
85
- return "⏳ Model is loading... Please wait 20-30 seconds and try again."
86
- elif response.status_code == 401:
87
- return "⚠️ Authentication Error: Your HF_TOKEN might be invalid. Please check it in Space Settings > Variables and secrets."
88
  else:
89
- return f"⚠️ API Error (Code {response.status_code}): {response.text}"
90
 
91
- except ImportError:
92
- return "ERROR: 'requests' library not found. Please add 'requests' to requirements.txt"
93
  except Exception as e:
94
  return f"ERROR: {str(e)}"
95
 
@@ -99,8 +103,18 @@ with gr.Blocks(title="VYZ Interstellar Multi-Tool AI", theme=gr.themes.Soft()) a
99
  gr.Markdown("# 🚀 VYZ Interstellar AI Assistant")
100
  gr.Markdown("A specialized, multi-tool assistant for VYZVERSE, ReVYZGen, and VYZENVO projects. **(Powered by Mistral 7B via HF Inference API)**")
101
 
 
 
 
102
  if not HF_TOKEN:
103
- gr.Markdown("### ⚠️ Warning: No HF_TOKEN found. Add it in Settings > Variables and secrets > New secret (Name: HF_TOKEN)")
 
 
 
 
 
 
 
104
 
105
  with gr.Tabs() as tabs:
106
  # TAB 1: VYZVERSE (Animation/Lore Generation)
 
1
  import gradio as gr
2
  import os
3
+ import requests
4
 
5
  # Try multiple ways to get the token
6
  HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HF_API_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
7
 
8
+ # Debug: Check if token exists (don't print the actual token!)
9
+ TOKEN_STATUS = "✅ Token found" if HF_TOKEN else "❌ No token found"
10
+
11
  def generate_output(project_name, user_input, file_path=None):
12
  """Generates specialized output using the LLM based on the project context."""
13
 
 
55
 
56
  # Call the HF Inference API using requests
57
  try:
 
 
58
  API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
59
 
60
+ headers = {"Content-Type": "application/json"}
61
  if HF_TOKEN:
62
  headers["Authorization"] = f"Bearer {HF_TOKEN}"
63
+ else:
64
+ return "❌ ERROR: No HF_TOKEN found!\n\nPlease add your Hugging Face token:\n1. Go to Space Settings\n2. Click 'Variables and secrets'\n3. Add new secret: Name='HF_TOKEN', Value=your token from https://huggingface.co/settings/tokens"
65
 
66
  # Format the prompt for Mistral
67
  prompt = f"<s>[INST] {system_prompt}\n\n{user_input} [/INST]"
 
82
  result = response.json()
83
  if isinstance(result, list) and len(result) > 0:
84
  generated_text = result[0].get('generated_text', '').strip()
85
+ return generated_text if generated_text else "Error: Empty response from model"
86
  else:
87
+ return f"Error: Unexpected response format: {result}"
88
  elif response.status_code == 503:
89
+ return "⏳ Model is loading... This can take 20-60 seconds on first use. Please try again in a moment."
90
+ elif response.status_code == 401 or response.status_code == 403:
91
+ return f"⚠️ Authentication Error (Code {response.status_code}): Your HF_TOKEN is invalid or expired.\n\n1. Get a new token from https://huggingface.co/settings/tokens\n2. Make sure to select 'Read' permission\n3. Update the HF_TOKEN secret in Space Settings"
92
  else:
93
+ return f"⚠️ API Error (Code {response.status_code}):\n{response.text[:500]}"
94
 
95
+ except requests.exceptions.Timeout:
96
+ return "⏳ Request timed out. The model might be loading. Please try again in 30 seconds."
97
  except Exception as e:
98
  return f"ERROR: {str(e)}"
99
 
 
103
  gr.Markdown("# 🚀 VYZ Interstellar AI Assistant")
104
  gr.Markdown("A specialized, multi-tool assistant for VYZVERSE, ReVYZGen, and VYZENVO projects. **(Powered by Mistral 7B via HF Inference API)**")
105
 
106
+ # Show token status
107
+ gr.Markdown(f"**Status:** {TOKEN_STATUS}")
108
+
109
  if not HF_TOKEN:
110
+ gr.Markdown("""
111
+ ### ⚠️ Setup Required:
112
+ 1. Go to https://huggingface.co/settings/tokens
113
+ 2. Create a new token with 'Read' permission
114
+ 3. Go to your Space Settings → Variables and secrets
115
+ 4. Add new secret: Name = `HF_TOKEN`, Value = your token
116
+ 5. Restart the Space
117
+ """)
118
 
119
  with gr.Tabs() as tabs:
120
  # TAB 1: VYZVERSE (Animation/Lore Generation)