abdullahalioo commited on
Commit
194ad81
·
verified ·
1 Parent(s): 39a5e51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -42
app.py CHANGED
@@ -1,35 +1,20 @@
1
  import os
2
- from fastapi import FastAPI, HTTPException
3
  from fastapi.responses import StreamingResponse
4
  from openai import AsyncOpenAI
5
- from pydantic import BaseModel
6
- import httpx
7
 
8
- # Initialize FastAPI app
9
  app = FastAPI()
10
 
11
- # Define request body model for the prompt
12
- class PromptRequest(BaseModel):
13
- prompt: str
 
 
14
 
15
- # Initialize OpenAI client
16
- token = os.getenv("GITHUB_TOKEN")
17
- if not token:
18
- raise ValueError("GITHUB_TOKEN environment variable not set")
19
 
20
- # Use environment variables for endpoint and model, with fallbacks
21
- endpoint = os.getenv("API_ENDPOINT", "https://api.openai.com/v1") # Fallback to OpenAI-compatible endpoint
22
- model = os.getenv("MODEL_NAME", "gpt-4o-mini") # Default to a known model
23
-
24
- # Initialize AsyncOpenAI with a custom HTTP client to avoid proxies issue
25
- client = AsyncOpenAI(
26
- base_url=endpoint,
27
- api_key=token,
28
- http_client=httpx.AsyncClient() # Explicitly use httpx.AsyncClient without proxies
29
- )
30
-
31
- # Async generator to stream chunks
32
- async def stream_response(prompt: str):
33
  try:
34
  # Create streaming chat completion
35
  stream = await client.chat.completions.create(
@@ -43,28 +28,21 @@ async def stream_response(prompt: str):
43
  stream=True
44
  )
45
 
46
- # Yield each chunk as it arrives
47
  async for chunk in stream:
48
  if chunk.choices and len(chunk.choices) > 0:
49
  content = chunk.choices[0].delta.content or ""
50
- if content:
51
- yield content
52
 
53
  except Exception as err:
54
- yield f"Error: {str(err)}"
55
 
56
- # Endpoint to handle prompt and stream response
57
  @app.post("/generate")
58
- async def generate_response(request: PromptRequest):
59
- try:
60
- return StreamingResponse(
61
- stream_response(request.prompt),
62
- media_type="text/event-stream"
63
- )
64
- except Exception as err:
65
- raise HTTPException(status_code=500, detail=f"Server error: {str(err)}")
66
-
67
- # Health check endpoint for Hugging Face Spaces
68
- @app.get("/")
69
- async def health_check():
70
- return {"status": "healthy"}
 
1
  import os
2
+ from fastapi import FastAPI, Request
3
  from fastapi.responses import StreamingResponse
4
  from openai import AsyncOpenAI
5
+ import asyncio
 
6
 
 
7
  app = FastAPI()
8
 
9
+ async def generate_ai_response(prompt: str):
10
+ # Get GitHub token from environment variable
11
+ token = os.getenv("GITHUB_TOKEN")
12
+ endpoint = "https://models.github.ai/inference"
13
+ model = "openai/gpt-4-1-mini" # Fixed typo in model name (was gpt-4.1-mini)
14
 
15
+ # Initialize OpenAI client
16
+ client = AsyncOpenAI(base_url=endpoint, api_key=token)
 
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  try:
19
  # Create streaming chat completion
20
  stream = await client.chat.completions.create(
 
28
  stream=True
29
  )
30
 
31
+ # Process the stream
32
  async for chunk in stream:
33
  if chunk.choices and len(chunk.choices) > 0:
34
  content = chunk.choices[0].delta.content or ""
35
+ yield content
 
36
 
37
  except Exception as err:
38
+ yield f"The sample encountered an error: {err}"
39
 
 
40
  @app.post("/generate")
41
+ async def generate_response(request: Request):
42
+ data = await request.json()
43
+ prompt = data.get("prompt", "what is ai") # Default prompt if none provided
44
+
45
+ return StreamingResponse(
46
+ generate_ai_response(prompt),
47
+ media_type="text/event-stream"
48
+ )