Jamiiwej2903 commited on
Commit
ea9f7e5
·
verified ·
1 Parent(s): 9f26e80

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +15 -33
main.py CHANGED
@@ -1,59 +1,41 @@
1
  import asyncio
2
  import aiohttp
3
- from fastapi import FastAPI, BackgroundTasks
4
- from pydantic import BaseModel
5
- from datetime import datetime
6
  import logging
7
 
8
- # Set up logging
9
  logging.basicConfig(level=logging.INFO)
10
  logger = logging.getLogger(__name__)
11
 
12
  app = FastAPI()
13
 
14
- # The URL of the API we want to call
15
  API_URL = "https://b917-160-179-178-105.ngrok-free.app/test-ai-call"
16
 
 
 
17
  async def call_api():
 
18
  async with aiohttp.ClientSession() as session:
19
  while True:
20
  try:
21
- async with session.get(API_URL) as response:
 
 
 
22
  if response.status == 200:
23
  data = await response.json()
24
- logger.info(f"API Response: {data}")
25
  else:
26
- logger.error(f"API call failed with status code: {response.status}")
 
27
  except Exception as e:
28
- logger.error(f"Error calling API: {e}")
29
-
30
- # Wait for 10 seconds before the next call
31
- await asyncio.sleep(10)
32
 
33
  @app.on_event("startup")
34
  async def startup_event():
35
- # Start the background task when the app starts
36
  asyncio.create_task(call_api())
37
 
38
  @app.get("/")
39
  async def root():
40
- return {"message": "API caller is running"}
41
-
42
- # You can keep your existing endpoints if needed
43
- # For example:
44
-
45
- class Item(BaseModel):
46
- prompt: str
47
- history: list
48
- system_prompt: str
49
- temperature: float = 0.0
50
- max_new_tokens: int = 1048
51
- top_p: float = 0.15
52
- repetition_penalty: float = 1.0
53
-
54
- @app.post("/generate/")
55
- async def generate_text(item: Item):
56
- # Your existing generate_text logic here
57
- return {"message": "Text generation endpoint"}
58
-
59
- # Add any other endpoints or functionality you need
 
1
  import asyncio
2
  import aiohttp
3
+ from fastapi import FastAPI
 
 
4
  import logging
5
 
 
6
  logging.basicConfig(level=logging.INFO)
7
  logger = logging.getLogger(__name__)
8
 
9
  app = FastAPI()
10
 
 
11
  API_URL = "https://b917-160-179-178-105.ngrok-free.app/test-ai-call"
12
 
13
+ call_count = 0
14
+
15
  async def call_api():
16
+ global call_count
17
  async with aiohttp.ClientSession() as session:
18
  while True:
19
  try:
20
+ call_count += 1
21
+ payload = {"count": call_count}
22
+
23
+ async with session.post(API_URL, json=payload) as response:
24
  if response.status == 200:
25
  data = await response.json()
26
+ logger.info(f"Call {call_count}: API Response: {data}")
27
  else:
28
+ logger.error(f"Call {call_count}: API call failed with status code: {response.status}")
29
+
30
  except Exception as e:
31
+ logger.error(f"Call {call_count}: Error: {e}")
32
+
33
+ await asyncio.sleep(1)
 
34
 
35
  @app.on_event("startup")
36
  async def startup_event():
 
37
  asyncio.create_task(call_api())
38
 
39
  @app.get("/")
40
  async def root():
41
+ return {"message": "API caller is running", "calls_made": call_count}