File size: 1,301 Bytes
f7c0abb
045ef7e
471a43e
d0fc55f
93c4b1f
f7c0abb
 
 
93c4b1f
 
d73495d
93c4b1f
f7c0abb
d0fc55f
93c4b1f
f7c0abb
 
 
 
93c4b1f
d0fc55f
f7c0abb
 
d0fc55f
045ef7e
 
f7c0abb
 
045ef7e
 
f7c0abb
 
045ef7e
 
 
 
471a43e
93c4b1f
 
7a83ce6
20d0b59
93c4b1f
045ef7e
93c4b1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
from fastapi import FastAPI, HTTPException
from fastapi.responses import StreamingResponse
from openai import AsyncOpenAI
import asyncio

app = FastAPI()

# Initialize OpenAI client
client = AsyncOpenAI(api_key=os.getenv("GITHUB_TOKEN"))

async def generate_ai_response(prompt: str):
    try:
        stream = await client.chat.completions.create(
            model="gpt-3.5-turbo",  # Using 3.5-turbo for better compatibility
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": prompt}
            ],
            temperature=0.7,
            stream=True
        )

        async for chunk in stream:
            if chunk.choices and chunk.choices[0].delta.content:
                yield chunk.choices[0].delta.content

    except Exception as err:
        yield f"Error: {str(err)}"
        raise HTTPException(status_code=500, detail="AI generation failed")

@app.post("/generate")
async def generate_response(prompt: str):
    if not prompt:
        raise HTTPException(status_code=400, detail="Prompt cannot be empty")
    
    return StreamingResponse(
        generate_ai_response(prompt),
        media_type="text/event-stream"
    )

# For Hugging Face Spaces
def get_app():
    return app