asimsultan commited on
Commit
2f4e610
·
1 Parent(s): 11076fa

Initial chatbot app with Gemma

Browse files
Files changed (3) hide show
  1. Dockerfile +13 -0
  2. app.py +36 -0
  3. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dockerfile
2
+ FROM python:3.10-slim
3
+
4
+ WORKDIR /app
5
+
6
+ COPY requirements.txt .
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ COPY app.py .
10
+
11
+ ENV PORT=8080
12
+
13
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from pydantic import BaseModel
4
+ from huggingface_hub import InferenceClient
5
+ import os
6
+
7
+ HF_TOKEN = os.getenv("HF_TOKEN")
8
+ MODEL_ID = "google/gemma-2b-it"
9
+ client = InferenceClient(token=HF_TOKEN)
10
+
11
+ app = FastAPI()
12
+
13
+ # Allow CORS for all origins (for development and Netlify)
14
+ app.add_middleware(
15
+ CORSMiddleware,
16
+ allow_origins=["*"], # For production, specify frontend domain
17
+ allow_credentials=True,
18
+ allow_methods=["*"],
19
+ allow_headers=["*"],
20
+ )
21
+
22
+ class ChatRequest(BaseModel):
23
+ message: str
24
+
25
+ @app.post("/chat")
26
+ async def chat(req: ChatRequest):
27
+ try:
28
+ messages = [{"role": "user", "content": req.message}]
29
+ response = client.chat_completion(
30
+ model=MODEL_ID,
31
+ messages=messages,
32
+ temperature=0.7,
33
+ )
34
+ return {"response": response.choices[0].message.content}
35
+ except Exception as e:
36
+ return {"error": str(e)}
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ huggingface_hub
4
+ python-dotenv