sonygod commited on
Commit
3c7203e
·
1 Parent(s): 34a8b9f
Files changed (4) hide show
  1. Dockerfile +12 -1
  2. app.py +22 -0
  3. requirements.txt +1 -0
  4. templates/chat.html +36 -0
Dockerfile CHANGED
@@ -2,8 +2,17 @@ FROM python:3.9-slim
2
 
3
  WORKDIR /app
4
 
 
 
 
 
 
 
 
 
5
  COPY ./requirements.txt /app/requirements.txt
6
  COPY ./app.py /app/app.py
 
7
 
8
 
9
  RUN pip install --no-cache-dir -r requirements.txt
@@ -11,4 +20,6 @@ RUN pip install --no-cache-dir -r requirements.txt
11
 
12
  EXPOSE 7860
13
 
14
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
2
 
3
  WORKDIR /app
4
 
5
+ # Install curl and other dependencies
6
+ RUN apt-get update && apt-get install -y \
7
+ curl \
8
+ && rm -rf /var/lib/apt/lists/*
9
+
10
+ # Install Ollama
11
+ RUN curl -fsSL https://ollama.com/install.sh | sh
12
+
13
  COPY ./requirements.txt /app/requirements.txt
14
  COPY ./app.py /app/app.py
15
+ COPY ./templates /app/templates
16
 
17
 
18
  RUN pip install --no-cache-dir -r requirements.txt
 
20
 
21
  EXPOSE 7860
22
 
23
+ #CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
24
+
25
+ CMD ollama serve & sleep 5 && ollama pull gemma:2b && uvicorn app:app --host 0.0.0.0 --port 7860
app.py CHANGED
@@ -23,6 +23,10 @@ import tempfile
23
  import re
24
  import random
25
  import aiohttp
 
 
 
 
26
  app = FastAPI()
27
 
28
  # Add USER_AGENTS constant
@@ -467,5 +471,23 @@ async def proxy_request(url: str, request: Request):
467
  logger.error(f"Proxy error: {str(e)}")
468
  raise HTTPException(status_code=500, detail=str(e))
469
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470
  if __name__ == "__main__":
471
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
23
  import re
24
  import random
25
  import aiohttp
26
+
27
+ from fastapi.templating import Jinja2Templates
28
+ from fastapi.staticfiles import StaticFiles
29
+ import ollama
30
  app = FastAPI()
31
 
32
  # Add USER_AGENTS constant
 
471
  logger.error(f"Proxy error: {str(e)}")
472
  raise HTTPException(status_code=500, detail=str(e))
473
 
474
+ templates = Jinja2Templates(directory="templates")
475
+
476
+ @app.get("/chat")
477
+ async def chat_page(request: Request):
478
+ return templates.TemplateResponse("chat.html", {"request": request})
479
+
480
+ @app.post("/testchat")
481
+ async def test_chat(data: dict):
482
+ try:
483
+ response = ollama.chat(model='gemma:2b', messages=[
484
+ {
485
+ 'role': 'user',
486
+ 'content': data['prompt']
487
+ }
488
+ ])
489
+ return {"response": response['message']['content']}
490
+ except Exception as e:
491
+ raise HTTPException(status_code=500, detail=str(e))
492
  if __name__ == "__main__":
493
  uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt CHANGED
@@ -5,3 +5,4 @@ python-multipart
5
  pydantic
6
  psutil
7
  aiohttp>=3.8.0
 
 
5
  pydantic
6
  psutil
7
  aiohttp>=3.8.0
8
+ ollama
templates/chat.html ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Ollama Chat</title>
5
+ </head>
6
+ <body>
7
+ <div>
8
+ <h1>Ollama Chat Test</h1>
9
+ <div>
10
+ <textarea id="prompt" rows="4" cols="50"></textarea>
11
+ </div>
12
+ <button onclick="sendMessage()">Send</button>
13
+ <div id="response"></div>
14
+ </div>
15
+
16
+ <script>
17
+ async function sendMessage() {
18
+ const prompt = document.getElementById('prompt').value;
19
+ const response = document.getElementById('response');
20
+ response.textContent = 'Loading...';
21
+
22
+ try {
23
+ const res = await fetch('/testchat', {
24
+ method: 'POST',
25
+ headers: {'Content-Type': 'application/json'},
26
+ body: JSON.stringify({prompt})
27
+ });
28
+ const data = await res.json();
29
+ response.textContent = data.response;
30
+ } catch (err) {
31
+ response.textContent = 'Error: ' + err;
32
+ }
33
+ }
34
+ </script>
35
+ </body>
36
+ </html>