Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import os
|
2 |
from fastapi import FastAPI, HTTPException
|
3 |
from fastapi.responses import StreamingResponse
|
4 |
-
|
5 |
from pydantic import BaseModel
|
6 |
|
7 |
# Initialize FastAPI app
|
@@ -16,44 +16,34 @@ token = os.getenv("GITHUB_TOKEN")
|
|
16 |
if not token:
|
17 |
raise ValueError("GITHUB_TOKEN environment variable not set")
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
model = os.getenv("MODEL_NAME", "gpt-4o-mini") # Use a valid model name, e.g., gpt-4o-mini or equivalent
|
22 |
|
23 |
-
#
|
24 |
-
client = AsyncOpenAI(
|
25 |
-
base_url=endpoint,
|
26 |
-
api_key=token,
|
27 |
-
# Explicitly disable proxies if not needed
|
28 |
-
http_client=None # Avoid passing unexpected kwargs like proxies
|
29 |
-
)
|
30 |
-
|
31 |
-
# Async generator to stream chunks
|
32 |
async def stream_response(prompt: str):
|
33 |
try:
|
34 |
-
# Create streaming chat completion
|
35 |
-
|
|
|
36 |
messages=[
|
37 |
{"role": "system", "content": "You are a helpful assistant."},
|
38 |
{"role": "user", "content": prompt}
|
39 |
],
|
40 |
temperature=1.0,
|
41 |
top_p=1.0,
|
42 |
-
|
43 |
-
stream=True
|
44 |
)
|
45 |
|
46 |
-
# Yield each chunk as it arrives
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
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:
|
@@ -68,4 +58,4 @@ async def generate_response(request: PromptRequest):
|
|
68 |
# Health check endpoint for Hugging Face Spaces
|
69 |
@app.get("/")
|
70 |
async def health_check():
|
71 |
-
return {"status": "healthy"}
|
|
|
1 |
import os
|
2 |
from fastapi import FastAPI, HTTPException
|
3 |
from fastapi.responses import StreamingResponse
|
4 |
+
import openai # Use OpenAI's official API library
|
5 |
from pydantic import BaseModel
|
6 |
|
7 |
# Initialize FastAPI app
|
|
|
16 |
if not token:
|
17 |
raise ValueError("GITHUB_TOKEN environment variable not set")
|
18 |
|
19 |
+
# Initialize OpenAI API client with API key
|
20 |
+
openai.api_key = token # Set the OpenAI API key
|
|
|
21 |
|
22 |
+
# Async generator to stream chunks from OpenAI's API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
async def stream_response(prompt: str):
|
24 |
try:
|
25 |
+
# Create streaming chat completion with OpenAI API
|
26 |
+
response = openai.ChatCompletion.create(
|
27 |
+
model="gpt-4", # Replace with the model you're using (e.g., gpt-3.5-turbo or gpt-4)
|
28 |
messages=[
|
29 |
{"role": "system", "content": "You are a helpful assistant."},
|
30 |
{"role": "user", "content": prompt}
|
31 |
],
|
32 |
temperature=1.0,
|
33 |
top_p=1.0,
|
34 |
+
stream=True # Enable streaming
|
|
|
35 |
)
|
36 |
|
37 |
+
# Yield each chunk of the response as it arrives
|
38 |
+
for chunk in response:
|
39 |
+
content = chunk.get("choices", [{}])[0].get("delta", {}).get("content", "")
|
40 |
+
if content:
|
41 |
+
yield content # Yield the generated content
|
|
|
42 |
|
43 |
except Exception as err:
|
44 |
yield f"Error: {str(err)}"
|
45 |
|
46 |
+
# Endpoint to handle the prompt and stream response
|
47 |
@app.post("/generate")
|
48 |
async def generate_response(request: PromptRequest):
|
49 |
try:
|
|
|
58 |
# Health check endpoint for Hugging Face Spaces
|
59 |
@app.get("/")
|
60 |
async def health_check():
|
61 |
+
return {"status": "healthy"}
|