Spaces:
Build error
Build error
import asyncio | |
import aiohttp | |
from fastapi import FastAPI | |
from sse_starlette.sse import EventSourceResponse | |
import logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
app = FastAPI() | |
API_URL = "https://b917-160-179-178-105.ngrok-free.app/test-ai-call" | |
call_count = 0 | |
async def event_generator(): | |
global call_count | |
async with aiohttp.ClientSession() as session: | |
while True: | |
try: | |
call_count += 1 | |
payload = {"count": call_count} | |
async with session.post(API_URL, json=payload) as response: | |
if response.status == 200: | |
data = await response.json() | |
logger.info(f"Call {call_count}: API Response: {data}") | |
yield data | |
else: | |
logger.error(f"Call {call_count}: API call failed with status code: {response.status}") | |
except Exception as e: | |
logger.error(f"Call {call_count}: Error: {e}") | |
await asyncio.sleep(10) | |
async def stream(request): | |
return EventSourceResponse(event_generator()) | |
async def root(): | |
return {"message": "API caller is running", "calls_made": call_count} |