kingkw1 commited on
Commit
892984a
·
1 Parent(s): 83c578c

feat: implement dynamic environment detection to handle local and Hugging Face deployment logic

Browse files
Files changed (1) hide show
  1. app.py +31 -25
app.py CHANGED
@@ -15,10 +15,16 @@ from langgraph.graph.message import add_messages
15
  from langgraph.prebuilt import tools_condition, ToolNode
16
  from langchain_core.messages import AnyMessage, HumanMessage, SystemMessage, ToolMessage
17
  from langchain_groq import ChatGroq
18
- # --- LOCAL RUN SETTINGS (Using langchain_ollama for local Ollama support) ---
19
- from langchain_ollama import ChatOllama
20
- # --- HUGGINGFACE SETTINGS (Uncomment for HF if needed, but langchain_ollama is preferred) ---
21
- # from langchain_community.chat_models import ChatOllama
 
 
 
 
 
 
22
 
23
  # (Keep Constants as is)
24
  # --- Constants ---
@@ -144,16 +150,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
144
  Fetches all questions, runs the BasicAgent on them, submits all answers,
145
  and displays the results.
146
  """
147
- # --- LOCAL RUN SETTINGS ---
148
- # Set a default username for local testing
149
- username = "local_user"
150
-
151
- # --- HUGGINGFACE SETTINGS (Comment out above and uncomment below for HF) ---
152
- # if profile:
153
- # username = f"{profile.username}"
154
- # print(f"{username} logged in")
155
- # else:
156
- # return "Please login to Hugging Face", None
157
 
158
  api_url = DEFAULT_API_URL
159
  questions_url = f"{api_url}/questions"
@@ -165,11 +171,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
165
  except Exception as e:
166
  print(f"Error instantiating agent: {e}")
167
  return f"Error initializing agent: {e}", None
168
- # --- LOCAL RUN SETTINGS ---
169
- space_id = os.getenv("SPACE_ID", "local-test-space")
170
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id != "local-test-space" else "local-execution"
171
- # --- END LOCAL RUN SETTINGS ---
172
-
 
 
173
  print(f"Agent code link: {agent_code}")
174
 
175
  # 2. Fetch Questions
@@ -207,12 +215,11 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
207
  print(f"Skipping item with missing task_id or question: {item}")
208
  continue
209
 
210
- # --- LOCAL RUN SETTINGS: Skip questions with files ---
211
- if file_name:
212
  print(f"Skipping task {task_id} because it requires a file: {file_name}")
213
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": "SKIPPED (Local run - No files)"})
214
  continue
215
- # --- END LOCAL RUN SETTINGS ---
216
 
217
  try:
218
  submitted_answer = agent(question_text, file_name)
@@ -322,9 +329,8 @@ with gr.Blocks() as demo:
322
  """
323
  )
324
 
325
- # --- LOCAL RUN SETTINGS (Comment out for HF) ---
326
- # gr.LoginButton()
327
- # --- END LOCAL RUN SETTINGS ---
328
 
329
  run_button = gr.Button("Run Full Evaluation & Submit All Answers", variant="primary")
330
 
 
15
  from langgraph.prebuilt import tools_condition, ToolNode
16
  from langchain_core.messages import AnyMessage, HumanMessage, SystemMessage, ToolMessage
17
  from langchain_groq import ChatGroq
18
+ # Detect if running locally or on Hugging Face Spaces
19
+ IS_LOCAL = os.getenv("SPACE_ID") is None
20
+
21
+ if IS_LOCAL:
22
+ from langchain_ollama import ChatOllama
23
+ else:
24
+ try:
25
+ from langchain_ollama import ChatOllama
26
+ except ImportError:
27
+ from langchain_community.chat_models import ChatOllama
28
 
29
  # (Keep Constants as is)
30
  # --- Constants ---
 
150
  Fetches all questions, runs the BasicAgent on them, submits all answers,
151
  and displays the results.
152
  """
153
+ if IS_LOCAL:
154
+ username = "local_user"
155
+ print(f"Running in LOCAL MODE. Using default username: {username}")
156
+ else:
157
+ if profile:
158
+ username = f"{profile.username}"
159
+ print(f"User logged in: {username}")
160
+ else:
161
+ print("User not logged in.")
162
+ return "Please Login to Hugging Face with the button.", None
163
 
164
  api_url = DEFAULT_API_URL
165
  questions_url = f"{api_url}/questions"
 
171
  except Exception as e:
172
  print(f"Error instantiating agent: {e}")
173
  return f"Error initializing agent: {e}", None
174
+ if IS_LOCAL:
175
+ space_id = "local-test-space"
176
+ agent_code = "local-execution"
177
+ else:
178
+ space_id = os.getenv("SPACE_ID")
179
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
180
+
181
  print(f"Agent code link: {agent_code}")
182
 
183
  # 2. Fetch Questions
 
215
  print(f"Skipping item with missing task_id or question: {item}")
216
  continue
217
 
218
+ # Skip questions with files if running locally
219
+ if IS_LOCAL and file_name:
220
  print(f"Skipping task {task_id} because it requires a file: {file_name}")
221
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": "SKIPPED (Local run - No files)"})
222
  continue
 
223
 
224
  try:
225
  submitted_answer = agent(question_text, file_name)
 
329
  """
330
  )
331
 
332
+ if not IS_LOCAL:
333
+ gr.LoginButton()
 
334
 
335
  run_button = gr.Button("Run Full Evaluation & Submit All Answers", variant="primary")
336