sushantvyz commited on
Commit
3543add
·
verified ·
1 Parent(s): f657f7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -18
app.py CHANGED
@@ -1,10 +1,8 @@
1
  import gradio as gr
2
  import os
3
- from huggingface_hub import InferenceClient
4
 
5
- # Use HF Inference API instead of loading model locally
6
- # This is FREE and doesn't require GPU/high memory
7
- client = InferenceClient()
8
 
9
  def generate_output(project_name, user_input, file_path=None):
10
  """Generates specialized output using the LLM based on the project context."""
@@ -51,31 +49,58 @@ def generate_output(project_name, user_input, file_path=None):
51
  system_prompt = "You are a general-purpose AI assistant. Respond helpfully."
52
  max_tokens = 250
53
 
54
- # Call the HF Inference API
55
  try:
56
- messages = [
57
- {"role": "system", "content": system_prompt},
58
- {"role": "user", "content": user_input}
59
- ]
60
 
61
- response = client.chat_completion(
62
- messages=messages,
63
- model="mistralai/Mistral-7B-Instruct-v0.3",
64
- max_tokens=max_tokens,
65
- temperature=0.7
66
- )
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- generated_text = response.choices[0].message.content.strip()
69
- return generated_text
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
 
 
71
  except Exception as e:
72
- return f"ERROR: Inference failed. Detail: {e}"
73
 
74
 
75
  # Build the Gradio Interface
76
  with gr.Blocks(title="VYZ Interstellar Multi-Tool AI", theme=gr.themes.Soft()) as demo:
77
  gr.Markdown("# 🚀 VYZ Interstellar AI Assistant")
78
  gr.Markdown("A specialized, multi-tool assistant for VYZVERSE, ReVYZGen, and VYZENVO projects. **(Powered by Mistral 7B via HF Inference API)**")
 
 
 
79
 
80
  with gr.Tabs() as tabs:
81
  # TAB 1: VYZVERSE (Animation/Lore Generation)
 
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."""
 
49
  system_prompt = "You are a general-purpose AI assistant. Respond helpfully."
50
  max_tokens = 250
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]"
64
+
65
+ payload = {
66
+ "inputs": prompt,
67
+ "parameters": {
68
+ "max_new_tokens": max_tokens,
69
+ "temperature": 0.7,
70
+ "top_p": 0.9,
71
+ "return_full_text": False
72
+ }
73
+ }
74
 
75
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
76
+
77
+ if response.status_code == 200:
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
 
96
 
97
  # Build the Gradio Interface
98
  with gr.Blocks(title="VYZ Interstellar Multi-Tool AI", theme=gr.themes.Soft()) as demo:
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)