initial commit
Browse files- .gitignore +53 -0
- Dockerfile +25 -0
- app.py +70 -0
- requirements.txt +3 -0
.gitignore
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
share/python-wheels/
|
| 24 |
+
*.egg-info/
|
| 25 |
+
.installed.cfg
|
| 26 |
+
*.egg
|
| 27 |
+
MANIFEST
|
| 28 |
+
|
| 29 |
+
# Virtual environments
|
| 30 |
+
venv/
|
| 31 |
+
ENV/
|
| 32 |
+
env/
|
| 33 |
+
.venv/
|
| 34 |
+
.env/
|
| 35 |
+
|
| 36 |
+
# Pytest cache
|
| 37 |
+
.pytest_cache/
|
| 38 |
+
|
| 39 |
+
# VS Code settings
|
| 40 |
+
.vscode/
|
| 41 |
+
|
| 42 |
+
# IDE settings
|
| 43 |
+
.idea/
|
| 44 |
+
|
| 45 |
+
# Logs
|
| 46 |
+
*.log
|
| 47 |
+
|
| 48 |
+
# OS files
|
| 49 |
+
.DS_Store
|
| 50 |
+
Thumbs.db
|
| 51 |
+
|
| 52 |
+
# Environment variables
|
| 53 |
+
.env
|
Dockerfile
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
# Create user
|
| 7 |
+
RUN useradd -m -u 1000 user
|
| 8 |
+
USER user
|
| 9 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 10 |
+
|
| 11 |
+
WORKDIR /app
|
| 12 |
+
|
| 13 |
+
# Install Ollama (CRITICAL FOR YOUR APP)
|
| 14 |
+
RUN wget -qO- https://ollama.com/install.sh | sh
|
| 15 |
+
# Pull your model
|
| 16 |
+
RUN ollama pull gpt-oss:20b
|
| 17 |
+
|
| 18 |
+
# Install Python dependencies
|
| 19 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 20 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 21 |
+
|
| 22 |
+
# Copy application code
|
| 23 |
+
COPY --chown=user . /app
|
| 24 |
+
|
| 25 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
+
from fastapi.responses import StreamingResponse, FileResponse
|
| 3 |
+
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
import httpx
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Serve chat.html at root
|
| 10 |
+
@app.get("/")
|
| 11 |
+
async def chat_page():
|
| 12 |
+
return FileResponse("templates/chat.html")
|
| 13 |
+
|
| 14 |
+
# Your existing streaming endpoint (simplified)
|
| 15 |
+
@app.post("/stream_chat")
|
| 16 |
+
async def stream_chat(request: Request):
|
| 17 |
+
data = await request.json()
|
| 18 |
+
prompt = data.get("prompt")
|
| 19 |
+
if not prompt:
|
| 20 |
+
raise HTTPException(status_code=400, detail="Missing 'prompt'")
|
| 21 |
+
|
| 22 |
+
# Use gpt-oss:2b as requested
|
| 23 |
+
model = "gpt-oss:20b"
|
| 24 |
+
|
| 25 |
+
async def event_generator():
|
| 26 |
+
try:
|
| 27 |
+
url = "http://localhost:11434/api/chat"
|
| 28 |
+
payload = {
|
| 29 |
+
"model": model,
|
| 30 |
+
"messages": [
|
| 31 |
+
{"role": "system", "content": "You are a thoughtful assistant."},
|
| 32 |
+
{"role": "user", "content": prompt}
|
| 33 |
+
],
|
| 34 |
+
"stream": True,
|
| 35 |
+
"options": {
|
| 36 |
+
"num_predict": 256,
|
| 37 |
+
"num_ctx": 4096
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
async with httpx.AsyncClient() as client:
|
| 42 |
+
async with client.stream("POST", url, json=payload, timeout=None) as resp:
|
| 43 |
+
resp.raise_for_status()
|
| 44 |
+
|
| 45 |
+
async for line in resp.aiter_lines():
|
| 46 |
+
if not line or not line.strip():
|
| 47 |
+
continue
|
| 48 |
+
try:
|
| 49 |
+
chunk = json.loads(line)
|
| 50 |
+
content = chunk.get("message", {}).get("content", "")
|
| 51 |
+
if content:
|
| 52 |
+
yield content
|
| 53 |
+
except json.JSONDecodeError:
|
| 54 |
+
continue
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
except httpx.HTTPStatusError as e:
|
| 58 |
+
# Capture Ollama-generated error
|
| 59 |
+
error_msg = f"Ollama API returned error: {e.response.status_code} - {e.response.text}"
|
| 60 |
+
yield f"[error]\n{error_msg}"
|
| 61 |
+
|
| 62 |
+
return StreamingResponse(
|
| 63 |
+
event_generator(),
|
| 64 |
+
media_type="text/plain"
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# For Hugging Face Spaces compatibility
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
import uvicorn
|
| 70 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
httpx
|
| 2 |
+
fastapi
|
| 3 |
+
uvicorn[standard]
|