Spaces:
Sleeping
Sleeping
Commit ·
9d3f61d
1
Parent(s): d34f0ce
fixes applied
Browse filesFiles changed (3)
- Dockerfile+2-2
- inference.py+114-138
- server/app.py+1-1
6 unmodified lines7891011126 unmodified linesCOPY . .EXPOSE 8000CMD ["uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "8000"]6 unmodified lines7891011126 unmodified linesCOPY . .EXPOSE 5000CMD ["uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "5000"]
39 unmodified lines40414243444546571 unmodified lines61861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576639 unmodified lines"api_base_url": os.getenv("API_BASE_URL", "http://localhost:11434/v1"),"model_name": os.getenv("MODEL_NAME", "llama2"),"hf_token": os.getenv("HF_TOKEN", ""),"env_url": os.getenv("ENV_URL", "http://localhost:5001"), # ✅ FIXED: Changed from 5000 to 5001"api_key": os.getenv("HF_TOKEN", "not-needed-for-local"),}return config571 unmodified linesexcept Exception as e:print(f"Warning: Could not initialize LLM client: {e}", file=sys.stderr)# Initialize variables for error handlingrewards = []step_num = 0action_str = "initialization"try:# Reset environmentreset_response = requests.post(f"{env_url}/reset",timeout=10)reset_response.raise_for_status()reset_data = reset_response.json()observation = reset_data.get("observation", {})task_name = observation.get("email_id", "email_workflow")email_subject = observation.get("subject", "")email_body = observation.get("body", "")customer_history = observation.get("customer_history", "")workflow_context = observation.get("previous_decisions", {}) # ✅ FIXED: Changed from "workflow_context" to "previous_decisions"# Log startlog_start(task_name, env_name, model_name)rewards = []step_num = 0done = False# Multi-step workflow loopwhile not done and step_num < 5:step_num += 1# Generate action based on current stepif step_num == 1:action = generate_classification_action(email_subject, email_body, customer_history, client, model_name)elif step_num == 2:classification = workflow_context.get("classification", "tech")action = generate_prioritization_action(email_subject, email_body, customer_history, classification, client, model_name)elif step_num == 3:classification = workflow_context.get("classification", "tech")priority = workflow_context.get("priority", "medium")sentiment = observation.get("customer_sentiment", "neutral") # ✅ FIXED: Use actual sentiment from observationaction = generate_strategy_action(email_subject, email_body, customer_history, classification, priority, sentiment, client, model_name)elif step_num == 4:classification = workflow_context.get("classification", "tech")priority = workflow_context.get("priority", "medium")strategy = workflow_context.get("strategy", "auto_resolve")action = generate_response_action(email_subject, email_body, customer_history, classification, priority, strategy, workflow_context, client, model_name)elif step_num == 5:action = generate_escalation_action(workflow_context, email_subject, email_body, customer_history, client, model_name)if action is None:# No escalation needed, end episodebreak# Convert action to string for loggingif action["action_type"] == "escalate":action_str = f"escalate_{action['content'].get('escalation_level', 'unknown')}"else:content_preview = str(action["content"])[:50].replace("\n", " ")action_str = f"{action['action_type']}:{content_preview}"# Step environmentstep_response = requests.post(f"{env_url}/step",json=action,timeout=15)step_response.raise_for_status()step_data = step_response.json()reward = step_data.get("reward", 0.0)done = step_data.get("done", True)info = step_data.get("info", {})# Update workflow context for next stepworkflow_context = info.get("workflow_state", workflow_context)rewards.append(reward)# Log steplog_step(step_num, action_str, reward, done, None)# Prepare final metricstotal_score = sum(rewards)success = total_score > 2.0 # Threshold for successful multi-step completion# CRITICAL FIX: Normalize score to [0,1] range as per OpenEnv specMAX_POSSIBLE_REWARD = 2.5 # Maximum theoretical score across all stepsnormalized_score = total_score / MAX_POSSIBLE_REWARDnormalized_score = min(max(normalized_score, 0.0), 1.0)# Log endlog_end(success, step_num, normalized_score, rewards)except requests.exceptions.RequestException as e:error_msg = f"Step {step_num} failed: {str(e)}"log_step(step_num, action_str, 0.0, False, error_msg)rewards.append(0.0)# Prepare final metrics after errortotal_score = sum(rewards)success = Falsenormalized_score = 0.0log_end(success, step_num, normalized_score, rewards)print(f"Error: {error_msg}", file=sys.stderr)return # Exit function instead of breakexcept Exception as e:error_msg = f"Step {step_num} error: {str(e)}"log_step(step_num, action_str, 0.0, False, error_msg)rewards.append(0.0)# Prepare final metrics after errortotal_score = sum(rewards)success = Falsenormalized_score = 0.0log_end(success, step_num, normalized_score, rewards)print(f"Error: {error_msg}", file=sys.stderr)return # Exit function instead of breakexcept requests.exceptions.RequestException as e:error_msg = f"Environment request failed: {str(e)}"log_start("error", env_name, model_name)log_step(1, "error", 0.0, False, error_msg)log_end(False, 1, 0.0, [0.0])print(f"Error: {error_msg}", file=sys.stderr)sys.exit(1)except Exception as e:error_msg = f"Inference failed: {str(e)}"log_start("error", env_name, model_name)log_step(1, "error", 0.0, False, error_msg)log_end(False, 1, 0.0, [0.0])print(f"Error: {error_msg}", file=sys.stderr)sys.exit(1)if __name__ == "__main__":39 unmodified lines40414243444546571 unmodified lines61861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174239 unmodified lines"api_base_url": os.getenv("API_BASE_URL", "http://localhost:11434/v1"),"model_name": os.getenv("MODEL_NAME", "llama2"),"hf_token": os.getenv("HF_TOKEN", ""),"env_url": os.getenv("ENV_URL", "http://localhost:5000"), # Fixed to match server default port"api_key": os.getenv("HF_TOKEN", "not-needed-for-local"),}return config571 unmodified linesexcept Exception as e:print(f"Warning: Could not initialize LLM client: {e}", file=sys.stderr)episode_count = 3for episode_idx in range(1, episode_count + 1):rewards = []step_num = 0action_str = "initialization"try:# Reset environmentreset_response = requests.post(f"{env_url}/reset",timeout=10)reset_response.raise_for_status()reset_data = reset_response.json()observation = reset_data.get("observation", {})task_name = observation.get("email_id", f"email_workflow_{episode_idx}")email_subject = observation.get("subject", "")email_body = observation.get("body", "")customer_history = observation.get("customer_history", "")workflow_context = observation.get("previous_decisions", {})# Log startlog_start(task_name, env_name, model_name)done = False# Multi-step workflow loopwhile not done and step_num < 5:step_num += 1# Generate action based on current stepif step_num == 1:action = generate_classification_action(email_subject, email_body, customer_history, client, model_name)elif step_num == 2:classification = workflow_context.get("classification", "tech")action = generate_prioritization_action(email_subject, email_body, customer_history, classification, client, model_name)elif step_num == 3:classification = workflow_context.get("classification", "tech")priority = workflow_context.get("priority", "medium")sentiment = observation.get("customer_sentiment", "neutral")action = generate_strategy_action(email_subject, email_body, customer_history, classification, priority, sentiment, client, model_name)elif step_num == 4:classification = workflow_context.get("classification", "tech")priority = workflow_context.get("priority", "medium")strategy = workflow_context.get("strategy", "auto_resolve")action = generate_response_action(email_subject, email_body, customer_history, classification, priority, strategy, workflow_context, client, model_name)elif step_num == 5:action = generate_escalation_action(workflow_context, email_subject, email_body, customer_history, client, model_name)if action is None:# No escalation needed, end episodedone = Truebreak# Convert action to string for loggingif action["action_type"] == "escalate":action_str = f"escalate_{action['content'].get('escalation_level', 'unknown')}"else:content_preview = str(action["content"])[:50].replace("\n", " ")action_str = f"{action['action_type']}:{content_preview}"# Step environmentstep_response = requests.post(f"{env_url}/step",json=action,timeout=15)step_response.raise_for_status()step_data = step_response.json()reward = step_data.get("reward", 0.0)done = step_data.get("done", True)info = step_data.get("info", {})# Update workflow context for next stepworkflow_context = info.get("workflow_state", workflow_context)rewards.append(reward)# Log steplog_step(step_num, action_str, reward, done, None)# Prepare final metricstotal_score = sum(rewards)MAX_POSSIBLE_REWARD = 2.5normalized_score = total_score / MAX_POSSIBLE_REWARDnormalized_score = min(max(normalized_score, 0.0), 1.0)success = normalized_score >= 0.7log_end(success, step_num, normalized_score, rewards)except requests.exceptions.RequestException as e:error_msg = f"Step {step_num} failed: {str(e)}"log_step(step_num, action_str, 0.0, False, error_msg)rewards.append(0.0)normalized_score = 0.0log_end(False, step_num, normalized_score, rewards)print(f"Error: {error_msg}", file=sys.stderr)continueexcept Exception as e:error_msg = f"Step {step_num} error: {str(e)}"log_step(step_num, action_str, 0.0, False, error_msg)rewards.append(0.0)normalized_score = 0.0log_end(False, step_num, normalized_score, rewards)print(f"Error: {error_msg}", file=sys.stderr)continueif __name__ == "__main__":
155 unmodified lines156157158159160161162155 unmodified linesdef main():"""Main entry point for running the server."""import uvicornuvicorn.run(app, host="0.0.0.0", port=5001)if __name__ == "__main__":155 unmodified lines156157158159160161162155 unmodified linesdef main():"""Main entry point for running the server."""import uvicornuvicorn.run(app, host="0.0.0.0", port=5000)if __name__ == "__main__":
Dockerfile+2-2
inference.py+114-138
server/app.py+1-1