shivamsshhiivvaamm commited on
Commit
28f20dc
·
verified ·
1 Parent(s): 804bd59

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +12 -0
  2. code.py +79 -0
  3. requirements.txt +3 -0
  4. tey.py +41 -0
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.13-slim
2
+
3
+ WORKDIR /ML
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY . .
9
+
10
+ EXPOSE 8000
11
+
12
+ CMD ["uvicorn", "code:app", "--host", "0.0.0.0", "--port", "8000"]
code.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.responses import HTMLResponse, StreamingResponse
3
+ from openai import OpenAI
4
+ from os import getenv
5
+ from dotenv import load_dotenv
6
+
7
+ load_dotenv()
8
+ app = FastAPI()
9
+
10
+ client = OpenAI(
11
+ base_url="https://openrouter.ai/api/v1",
12
+ api_key=getenv("OPENROUTER_API_KEY"),
13
+ )
14
+
15
+ @app.get("/", response_class=HTMLResponse)
16
+ def index():
17
+ return """
18
+ <!DOCTYPE html>
19
+ <html>
20
+ <head>
21
+ <title>Streaming Chat</title>
22
+ <style>
23
+ body { font-family: monospace; }
24
+ #output { white-space: pre-wrap; }
25
+ </style>
26
+ </head>
27
+ <body>
28
+ <h2>Streaming Response</h2>
29
+ <button onclick="startChat()">Start Chat</button>
30
+ <div id="output"></div>
31
+
32
+ <script>
33
+ async function startChat() {
34
+ const output = document.getElementById("output");
35
+ output.textContent = "";
36
+
37
+ const response = await fetch("/chat");
38
+
39
+ const reader = response.body.getReader();
40
+ const decoder = new TextDecoder();
41
+
42
+ while (true) {
43
+ const { value, done } = await reader.read();
44
+ if (done) break;
45
+
46
+ const chunk = decoder.decode(value, { stream: true });
47
+ output.textContent += chunk; // 🔥 live append
48
+ }
49
+ }
50
+ </script>
51
+ </body>
52
+ </html>
53
+ """
54
+
55
+ @app.get("/chat")
56
+ def chat():
57
+
58
+ def token_generator():
59
+ stream = client.chat.completions.create(
60
+ model="openai/gpt-oss-120b",
61
+ messages=[{"role": "user", "content": "Say you are a poet and write a poem about streaming responses. with the small description and include a dummy name to"}],
62
+ stream=True,
63
+ )
64
+
65
+ for event in stream:
66
+ delta = event.choices[0].delta
67
+ if delta and delta.content:
68
+ yield delta.content # ✅ streamed to frontend
69
+
70
+ return StreamingResponse(
71
+ token_generator(),
72
+ media_type="text/plain"
73
+ )
74
+
75
+
76
+
77
+ if __name__ == "__main__":
78
+ import uvicorn
79
+ uvicorn.run(app, host="0.0.0.0", port=8000)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ openai
tey.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ from openai import OpenAI
3
+ from dotenv import load_dotenv
4
+ from os import getenv
5
+
6
+ load_dotenv()
7
+
8
+ logging.basicConfig(level=logging.INFO)
9
+ logger = logging.getLogger(__name__)
10
+
11
+ client = OpenAI(
12
+ base_url="https://openrouter.ai/api/v1",
13
+ api_key=getenv("OPENROUTER_API_KEY"),
14
+ )
15
+
16
+ def chat():
17
+ stream = client.chat.completions.create(
18
+ model="openai/gpt-oss-120b",
19
+ messages=[
20
+ {"role": "user", "content": "Say this is a test"}
21
+ ],
22
+ stream=True, # ✅ REQUIRED
23
+ )
24
+
25
+ logger.info("Streaming response:")
26
+
27
+ full_response = ""
28
+
29
+ for event in stream:
30
+ # Some events don't contain text — always guard
31
+ delta = event.choices[0].delta
32
+ if delta and delta.content:
33
+ token = delta.content
34
+ logger.info(token)
35
+ print(token, end="", flush=True)
36
+ full_response += token
37
+
38
+ print()
39
+ return {"response": full_response}
40
+
41
+ chat()