Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- .gitattributes +1 -0
- Dockerfile +1 -1
- app/main.py +72 -0
- models/final-gemma2b_SA-Q5_K.gguf +3 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ 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
|
|
|
|
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
|
Dockerfile
CHANGED
@@ -16,4 +16,4 @@ EXPOSE 7860
|
|
16 |
|
17 |
ENV PYTHONUNBUFFERED=1
|
18 |
|
19 |
-
CMD ["uvicorn", "app.main:app", "--port", "7860", "--host", "0.0.0.0"]
|
|
|
16 |
|
17 |
ENV PYTHONUNBUFFERED=1
|
18 |
|
19 |
+
CMD ["uvicorn", "app.main:app", "--port", "7860", "--host", "0.0.0.0"]
|
app/main.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
from os import listdir
|
4 |
+
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(
|
11 |
+
model_path="/models/final-gemma2b_SA-Q5_K.gguf",
|
12 |
+
# n_gpu_layers=28, # Uncomment to use GPU acceleration
|
13 |
+
# seed=1337, # Uncomment to set a specific seed
|
14 |
+
# n_ctx=2048, # Uncomment to increase the context window
|
15 |
+
)
|
16 |
+
|
17 |
+
def ask(question, max_new_tokens=200):
|
18 |
+
output = llm(
|
19 |
+
question, # Prompt
|
20 |
+
max_tokens=max_new_tokens, # Generate up to 32 tokens, set to None to generate up to the end of the context window
|
21 |
+
stop=["\n"], # Stop generating just before the model would generate a new question
|
22 |
+
echo=False, # Echo the prompt back in the output
|
23 |
+
temperature=0.0,
|
24 |
+
)
|
25 |
+
return output
|
26 |
+
|
27 |
+
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.")
|
34 |
+
|
35 |
+
app = FastAPI(
|
36 |
+
title = "GemmaSA_2b",
|
37 |
+
description="A simple sentiment analysis API for the Thai language, powered by a finetuned version of Gemma-2b",
|
38 |
+
version="1.0.0",
|
39 |
+
)
|
40 |
+
|
41 |
+
origins = ["*"]
|
42 |
+
app.add_middleware(
|
43 |
+
CORSMiddleware,
|
44 |
+
allow_origins=origins,
|
45 |
+
allow_credentials=True,
|
46 |
+
allow_methods=["*"],
|
47 |
+
allow_headers=["*"]
|
48 |
+
)
|
49 |
+
|
50 |
+
@app.get('/')
|
51 |
+
def docs():
|
52 |
+
"Redirects the user from the main page to the docs."
|
53 |
+
return responses.RedirectResponse('./docs')
|
54 |
+
|
55 |
+
@app.get('/add/{a}/{b}')
|
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.")
|
models/final-gemma2b_SA-Q5_K.gguf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a138b3286615045657dc3e559ce97aed434e47bc8887113852e6281bdac9aed4
|
3 |
+
size 1839868832
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
uvicorn[standard]
|
2 |
+
fastapi
|
3 |
+
llama-cpp-python
|