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

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +43 -42
main.py CHANGED
@@ -1,11 +1,46 @@
1
- from fastapi import FastAPI
 
 
2
  from pydantic import BaseModel
3
- from huggingface_hub import InferenceClient
4
- from fastapi.responses import StreamingResponse
 
 
 
 
5
 
6
  app = FastAPI()
7
 
8
- client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  class Item(BaseModel):
11
  prompt: str
@@ -16,43 +51,9 @@ class Item(BaseModel):
16
  top_p: float = 0.15
17
  repetition_penalty: float = 1.0
18
 
19
- def format_prompt(message, history):
20
- prompt = "<s>"
21
- for user_prompt, bot_response in history:
22
- prompt += f"[INST] {user_prompt} [/INST]"
23
- prompt += f" {bot_response}</s> "
24
- prompt += f"[INST] {message} [/INST]"
25
- return prompt
26
-
27
- async def generate_stream(item: Item):
28
- try:
29
- temperature = max(float(item.temperature), 1e-2)
30
- top_p = float(item.top_p)
31
-
32
- generate_kwargs = dict(
33
- temperature=temperature,
34
- max_new_tokens=item.max_new_tokens,
35
- top_p=top_p,
36
- repetition_penalty=item.repetition_penalty,
37
- do_sample=True,
38
- seed=42,
39
- )
40
-
41
- formatted_prompt = format_prompt(f"{item.system_prompt}, {item.prompt}", item.history)
42
- stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
43
-
44
- for response in stream:
45
- yield response.token.text
46
- except Exception as e:
47
- print(f"Error in generate_stream: {e}")
48
- finally:
49
- if 'stream' in locals():
50
- stream.close()
51
-
52
  @app.post("/generate/")
53
  async def generate_text(item: Item):
54
- try:
55
- return StreamingResponse(generate_stream(item), media_type="text/plain")
56
- except Exception as e:
57
- print(f"Error in generate_text: {e}")
58
- return {"error": str(e)}
 
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
 
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