PawinC commited on
Commit
7afebb8
1 Parent(s): e730535

Upload 8 files

Browse files
Files changed (6) hide show
  1. .gitattributes +0 -2
  2. Dockerfile +3 -2
  3. app/main.py +39 -14
  4. doDockerStuff.sh +10 -0
  5. docker-compose.yml +11 -0
  6. sa-app.zip +3 -0
.gitattributes CHANGED
@@ -33,5 +33,3 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
- models/final-gemma2b_SA-Q5_K.gguf filter=lfs diff=lfs merge=lfs -text
37
- models/final-gemma2b_SA-Q8_0.gguf filter=lfs diff=lfs merge=lfs -text
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
Dockerfile CHANGED
@@ -10,9 +10,10 @@ RUN pip install -r requirements.txt
10
 
11
  COPY app /app
12
 
13
- COPY models /models
 
14
 
15
- EXPOSE 7860
16
 
17
  ENV PYTHONUNBUFFERED=1
18
 
 
10
 
11
  COPY app /app
12
 
13
+ COPY models /models
14
+ #DO NOT FORGET TO UNCOMMENT THE ABOVE WHEN PUSHING TO HF!!!!
15
 
16
+ # EXPOSE 7860
17
 
18
  ENV PYTHONUNBUFFERED=1
19
 
app/main.py CHANGED
@@ -5,6 +5,11 @@ from os.path import isdir
5
  from fastapi import FastAPI, HTTPException, Request, responses
6
  from fastapi.middleware.cors import CORSMiddleware
7
  from llama_cpp import Llama
 
 
 
 
 
8
 
9
  print("Loading model...")
10
  llm = Llama(
@@ -28,6 +33,15 @@ def check_sentiment(text):
28
  result = ask(f'Analyze the sentiment of the tweet enclosed in square brackets, determine if it is positive or negative, and return the answer as the corresponding sentiment label "positive" or "negative" [{text}] =', max_new_tokens=3)
29
  return result['choices'][0]['text'].strip()
30
 
 
 
 
 
 
 
 
 
 
31
  print("Testing model...")
32
  assert "positive" in check_sentiment("ดอกไม้ร้านนี้สวยจัง")
33
  print("Ready.")
@@ -47,6 +61,18 @@ app.add_middleware(
47
  allow_headers=["*"]
48
  )
49
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  @app.get('/')
51
  def docs():
52
  "Redirects the user from the main page to the docs."
@@ -56,17 +82,16 @@ def docs():
56
  def add(a: int,b: int):
57
  return a + b
58
 
59
- @app.get('/SA')
60
- def perform_sentiment_analysis(request: Request):
61
- """Performs a sentiment analysis using a finetuned version of Gemma-7b"""
62
- prompt = request.query_params.get('prompt')
63
- if prompt:
64
- try:
65
- print(f"Checking sentiment for {prompt}")
66
- result = check_sentiment(prompt)
67
- print(f"Result: {result}")
68
- return {'success': True, 'result': result}
69
- except Exception as e:
70
- return HTTPException(500, str(e))
71
- else:
72
- return HTTPException(400, "Request argument 'prompt' not provided.")
 
5
  from fastapi import FastAPI, HTTPException, Request, responses
6
  from fastapi.middleware.cors import CORSMiddleware
7
  from llama_cpp import Llama
8
+ from fastapi import Body
9
+
10
+ from pydantic import BaseModel
11
+ from enum import Enum
12
+ from typing import Optional
13
 
14
  print("Loading model...")
15
  llm = Llama(
 
33
  result = ask(f'Analyze the sentiment of the tweet enclosed in square brackets, determine if it is positive or negative, and return the answer as the corresponding sentiment label "positive" or "negative" [{text}] =', max_new_tokens=3)
34
  return result['choices'][0]['text'].strip()
35
 
36
+ def clean_sentiment_response(response_text):
37
+ result = response_text.strip()
38
+ if "positive" in result:
39
+ return "positive"
40
+ elif "negative" in result:
41
+ return "negative"
42
+ else:
43
+ return "unknown"
44
+
45
  print("Testing model...")
46
  assert "positive" in check_sentiment("ดอกไม้ร้านนี้สวยจัง")
47
  print("Ready.")
 
61
  allow_headers=["*"]
62
  )
63
 
64
+ class SA_Result(str, Enum):
65
+ positive = "positive"
66
+ negative = "negative"
67
+ unknown = "unknown"
68
+
69
+ class SA_Response(BaseModel):
70
+ text: Optional[str] = None
71
+ code: int = 200
72
+ result: SA_Result = None
73
+
74
+
75
+
76
  @app.get('/')
77
  def docs():
78
  "Redirects the user from the main page to the docs."
 
82
  def add(a: int,b: int):
83
  return a + b
84
 
85
+ @app.post('/SA')
86
+ def perform_sentiment_analysis(prompt: str = Body(..., embed=True, example="I like eating fried chicken")) -> SA_Response:
87
+ """Performs a sentiment analysis using a finetuned version of Gemma-7b"""
88
+ if prompt:
89
+ try:
90
+ print(f"Checking sentiment for {prompt}")
91
+ result = clean_sentiment_response(check_sentiment(prompt))
92
+ print(f"Result: {result}")
93
+ return SA_Response(result=result, text=prompt)
94
+ except Exception as e:
95
+ return HTTPException(500, SA_Response(code=500, result=str(e), text=prompt))
96
+ else:
97
+ return HTTPException(400, SA_Response(code=400, result="Request argument 'prompt' not provided."))
 
doDockerStuff.sh ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Build the Docker image
4
+ docker build -t gemmasa-2bi -f Dockerfile .
5
+
6
+ # The volumes thing
7
+ docker volume create --opt type=none --opt o=bind --opt device=/home/pawin/Code/GemmaSA_2B/models models
8
+
9
+ # Run the Docker container
10
+ docker compose up --force-recreate --remove-orphans
docker-compose.yml ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: '3.0'
2
+ services:
3
+ gemmasa-2bc: # Container's name
4
+ image: "gemmasa-2bi" # Image's name
5
+ container_name: "gemmasa-2bc" # Container's name
6
+ ports:
7
+ - 7860:7860
8
+ volumes:
9
+ - /home/pawin/Code/GemmaSA_2B/models:/models/
10
+ # volumes:
11
+ # - //D/git/oppabank/model_folder:
sa-app.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:672442dbd59d24f744e06e9b5c67f687ce4578729e1f7c1db3619fb660951db5
3
+ size 5848