test_consolidated_allergens.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import os
  2. import paramiko
  3. import dotenv
  4. repo_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  5. dotenv.load_dotenv(dotenv_path=os.path.join(repo_root, ".env"))
  6. host = os.environ.get('SERVER_HOST')
  7. user = os.environ.get('SERVER_USER')
  8. password = os.environ.get('SERVER_PASS')
  9. if password == "your_db_password_here" or not password:
  10. password = None
  11. ssh = paramiko.SSHClient()
  12. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  13. try:
  14. ssh.connect(host, username=user, password=password, timeout=10)
  15. print("SSH Connected!")
  16. python_cmd = """
  17. import ollama
  18. model = 'llama3.2:3b'
  19. candidates = ["milk chocolate", "peanuts", "sugar", "milk", "cocoa butter", "lactose", "snickers bar", "snickers"]
  20. prompt_lines = [
  21. "You are a food safety expert. For each item in the list below, answer the question exactly.",
  22. "Respond with 'Yes' or 'No'. Format the output exactly as:",
  23. "ItemName: Yes/No",
  24. "\\nQuestions:"
  25. ]
  26. for c in candidates:
  27. prompt_lines.append(f"Answer by yes or no, if it is in some case answer yes : Are {c} allergens.")
  28. prompt = "\\n".join(prompt_lines)
  29. print("--- Sending Consolidated Prompt ---")
  30. import time
  31. t0 = time.time()
  32. res = ollama.chat(model=model, messages=[{'role': 'user', 'content': prompt}])
  33. content = res['message']['content'].strip()
  34. print("Response:\\n", content)
  35. print("Time taken:", time.time() - t0)
  36. """
  37. # Write python_cmd to server
  38. sftp = ssh.open_sftp()
  39. with sftp.file('food_project/test_consolidated.py', 'w') as f:
  40. f.write(python_cmd)
  41. sftp.close()
  42. # Copy to container
  43. ssh.exec_command("docker cp food_project/test_consolidated.py food_project-app-1:/app/test_consolidated.py")
  44. stdin, stdout, stderr = ssh.exec_command("docker exec food_project-app-1 python /app/test_consolidated.py")
  45. print("STDOUT:\\n", stdout.read().decode('utf-8'))
  46. print("STDERR:\\n", stderr.read().decode('utf-8'))
  47. except Exception as e:
  48. print(f"Error: {e}")
  49. finally:
  50. ssh.close()