Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,44 @@
|
|
| 1 |
import os
|
| 2 |
-
from fastapi import FastAPI,
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
from openai import AsyncOpenAI
|
| 5 |
import asyncio
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
# Initialize OpenAI client
|
| 10 |
-
client = AsyncOpenAI(
|
| 11 |
-
api_key=os.getenv("GITHUB_TOKEN") # Get API key from environment variable
|
| 12 |
-
)
|
| 13 |
|
| 14 |
async def generate_ai_response(prompt: str):
|
| 15 |
try:
|
| 16 |
-
# Create streaming chat completion
|
| 17 |
stream = await client.chat.completions.create(
|
| 18 |
-
model="gpt-3.5-turbo", #
|
| 19 |
messages=[
|
| 20 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 21 |
{"role": "user", "content": prompt}
|
| 22 |
],
|
| 23 |
-
temperature=0.7,
|
| 24 |
-
top_p=1.0,
|
| 25 |
stream=True
|
| 26 |
)
|
| 27 |
|
| 28 |
-
# Process the stream
|
| 29 |
async for chunk in stream:
|
| 30 |
-
if chunk.choices and
|
| 31 |
-
|
| 32 |
-
yield content
|
| 33 |
|
| 34 |
except Exception as err:
|
| 35 |
-
yield f"Error
|
|
|
|
| 36 |
|
| 37 |
@app.post("/generate")
|
| 38 |
-
async def generate_response(
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return StreamingResponse(
|
| 47 |
-
generate_ai_response(prompt),
|
| 48 |
-
media_type="text/event-stream"
|
| 49 |
-
)
|
| 50 |
-
except Exception as e:
|
| 51 |
-
return {"error": f"Invalid request: {str(e)}"}, 400
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
from openai import AsyncOpenAI
|
| 5 |
import asyncio
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# Initialize OpenAI client
|
| 10 |
+
client = AsyncOpenAI(api_key=os.getenv("GITHUB_TOKEN"))
|
|
|
|
|
|
|
| 11 |
|
| 12 |
async def generate_ai_response(prompt: str):
|
| 13 |
try:
|
|
|
|
| 14 |
stream = await client.chat.completions.create(
|
| 15 |
+
model="gpt-3.5-turbo", # Using 3.5-turbo for better compatibility
|
| 16 |
messages=[
|
| 17 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 18 |
{"role": "user", "content": prompt}
|
| 19 |
],
|
| 20 |
+
temperature=0.7,
|
|
|
|
| 21 |
stream=True
|
| 22 |
)
|
| 23 |
|
|
|
|
| 24 |
async for chunk in stream:
|
| 25 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 26 |
+
yield chunk.choices[0].delta.content
|
|
|
|
| 27 |
|
| 28 |
except Exception as err:
|
| 29 |
+
yield f"Error: {str(err)}"
|
| 30 |
+
raise HTTPException(status_code=500, detail="AI generation failed")
|
| 31 |
|
| 32 |
@app.post("/generate")
|
| 33 |
+
async def generate_response(prompt: str):
|
| 34 |
+
if not prompt:
|
| 35 |
+
raise HTTPException(status_code=400, detail="Prompt cannot be empty")
|
| 36 |
+
|
| 37 |
+
return StreamingResponse(
|
| 38 |
+
generate_ai_response(prompt),
|
| 39 |
+
media_type="text/event-stream"
|
| 40 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# For Hugging Face Spaces
|
| 43 |
+
def get_app():
|
| 44 |
+
return app
|