test_egg.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. import json
  19. model = 'llama3.2:3b'
  20. unique_aliments = ["Egg"]
  21. aliments_str = ", ".join(unique_aliments)
  22. prompt = f"In these aliments : {aliments_str} are there allergens and if it is the case also said the allergen kinds. Return the answer as json array with two variables aliment and the associate allergen in an array find inside it. focus on the list, do not add or delete aliments."
  23. try:
  24. response = ollama.chat(
  25. model=model,
  26. messages=[{'role': 'user', 'content': prompt}],
  27. format='json'
  28. )
  29. res_content = response['message']['content'].strip()
  30. print("Response Content:\\n", res_content)
  31. data = json.loads(res_content)
  32. print("Parsed JSON:\\n", json.dumps(data, indent=2))
  33. except Exception as e:
  34. print("Error:", e)
  35. """
  36. sftp = ssh.open_sftp()
  37. with sftp.file('food_project/test_egg.py', 'w') as f:
  38. f.write(python_cmd)
  39. sftp.close()
  40. ssh.exec_command("docker cp food_project/test_egg.py food_project-app-1:/app/test_egg.py")
  41. stdin, stdout, stderr = ssh.exec_command("docker exec food_project-app-1 python /app/test_egg.py")
  42. print("STDOUT:\n", stdout.read().decode('utf-8'))
  43. print("STDERR:\n", stderr.read().decode('utf-8'))
  44. except Exception as e:
  45. print(f"Error: {e}")
  46. finally:
  47. ssh.close()