Spaces:
Running
Running
TahaFawzyElshrif commited on
Commit ·
5410b93
1
Parent(s): 29d79d6
debug
Browse files- Queue_Producer.py +31 -0
- app.py +25 -36
Queue_Producer.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Producer
|
| 2 |
+
import pika
|
| 3 |
+
import os
|
| 4 |
+
RABBITMQ_URL = os.environ["RABBITMQ_URL"]
|
| 5 |
+
QUEUE_NAME = os.environ["QUEUE_NAME"]
|
| 6 |
+
|
| 7 |
+
def get_connection():
|
| 8 |
+
params = pika.URLParameters(RABBITMQ_URL)
|
| 9 |
+
return pika.BlockingConnection(params)
|
| 10 |
+
|
| 11 |
+
connection = get_connection()
|
| 12 |
+
def send_message(data: dict):
|
| 13 |
+
connection = get_connection()
|
| 14 |
+
channel = connection.channel()
|
| 15 |
+
|
| 16 |
+
channel.queue_declare(queue=QUEUE_NAME, durable=True)
|
| 17 |
+
|
| 18 |
+
message = str(data)
|
| 19 |
+
|
| 20 |
+
channel.basic_publish(
|
| 21 |
+
exchange='',
|
| 22 |
+
routing_key=QUEUE_NAME,
|
| 23 |
+
body=message,
|
| 24 |
+
properties=pika.BasicProperties(
|
| 25 |
+
delivery_mode=2, # make message persistent
|
| 26 |
+
)
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
connection.close()
|
| 30 |
+
|
| 31 |
+
return {"status": "sent", "data": data}
|
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
import uvicorn
|
| 3 |
import sys
|
|
@@ -8,13 +10,19 @@ from Server import get_response
|
|
| 8 |
from pydantic import BaseModel
|
| 9 |
from agent.agent_graph.StateTasks import ProblemState
|
| 10 |
import subprocess
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
#
|
|
|
|
| 14 |
for i in range(3): # Start 3 consumers
|
| 15 |
subprocess.Popen(['python','-u','Consumer.py'])
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Create app instance
|
| 19 |
app = FastAPI()
|
| 20 |
print("Starting API Server...")
|
|
@@ -29,42 +37,15 @@ class RequestModel(BaseModel):
|
|
| 29 |
last_state : str
|
| 30 |
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
@app.get("/")
|
| 33 |
def read_root():
|
| 34 |
return {"message": "Hello From CodeBuddyAI!"}
|
| 35 |
|
| 36 |
-
|
| 37 |
-
# Producer
|
| 38 |
-
import pika
|
| 39 |
-
RABBITMQ_URL = os.environ["RABBITMQ_URL"]
|
| 40 |
-
QUEUE_NAME = os.environ["QUEUE_NAME"]
|
| 41 |
-
|
| 42 |
-
def get_connection():
|
| 43 |
-
params = pika.URLParameters(RABBITMQ_URL)
|
| 44 |
-
return pika.BlockingConnection(params)
|
| 45 |
-
|
| 46 |
-
connection = get_connection()
|
| 47 |
-
def send_message(data: dict):
|
| 48 |
-
connection = get_connection()
|
| 49 |
-
channel = connection.channel()
|
| 50 |
-
|
| 51 |
-
channel.queue_declare(queue=QUEUE_NAME, durable=True)
|
| 52 |
-
|
| 53 |
-
message = str(data)
|
| 54 |
-
|
| 55 |
-
channel.basic_publish(
|
| 56 |
-
exchange='',
|
| 57 |
-
routing_key=QUEUE_NAME,
|
| 58 |
-
body=message,
|
| 59 |
-
properties=pika.BasicProperties(
|
| 60 |
-
delivery_mode=2, # make message persistent
|
| 61 |
-
)
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
connection.close()
|
| 65 |
-
|
| 66 |
-
return {"status": "sent", "data": data}
|
| 67 |
-
######################
|
| 68 |
def old_call(request: RequestModel):
|
| 69 |
# fill with last state
|
| 70 |
try:
|
|
@@ -83,11 +64,19 @@ def old_call(request: RequestModel):
|
|
| 83 |
|
| 84 |
|
| 85 |
return {"Data": answer}
|
| 86 |
-
|
| 87 |
|
| 88 |
@app.post("/call/")
|
| 89 |
def call(request: RequestModel):
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
| 93 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
from urllib import request
|
| 2 |
+
|
| 3 |
from fastapi import FastAPI
|
| 4 |
import uvicorn
|
| 5 |
import sys
|
|
|
|
| 10 |
from pydantic import BaseModel
|
| 11 |
from agent.agent_graph.StateTasks import ProblemState
|
| 12 |
import subprocess
|
| 13 |
+
from Queue_Producer import send_message
|
| 14 |
|
| 15 |
+
##################################################
|
| 16 |
+
# START CONSUMERS in a separate process
|
| 17 |
+
##################################################
|
| 18 |
for i in range(3): # Start 3 consumers
|
| 19 |
subprocess.Popen(['python','-u','Consumer.py'])
|
| 20 |
|
| 21 |
|
| 22 |
+
##################################################
|
| 23 |
+
# START API
|
| 24 |
+
##################################################
|
| 25 |
+
|
| 26 |
# Create app instance
|
| 27 |
app = FastAPI()
|
| 28 |
print("Starting API Server...")
|
|
|
|
| 37 |
last_state : str
|
| 38 |
|
| 39 |
|
| 40 |
+
##################################################
|
| 41 |
+
# ROUTES
|
| 42 |
+
##################################################
|
| 43 |
+
|
| 44 |
@app.get("/")
|
| 45 |
def read_root():
|
| 46 |
return {"message": "Hello From CodeBuddyAI!"}
|
| 47 |
|
| 48 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def old_call(request: RequestModel):
|
| 50 |
# fill with last state
|
| 51 |
try:
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
return {"Data": answer}
|
| 67 |
+
"""
|
| 68 |
|
| 69 |
@app.post("/call/")
|
| 70 |
def call(request: RequestModel):
|
| 71 |
+
try:
|
| 72 |
+
state = json.loads(request.last_state)
|
| 73 |
+
except Exception:
|
| 74 |
+
state: ProblemState = {
|
| 75 |
+
"question": request.prompt,
|
| 76 |
+
"memory": request.memory
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
return send_message(state)
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|