Spaces:
Sleeping
Sleeping
added Llama3
Browse files
app.py
CHANGED
|
@@ -3,8 +3,11 @@ from transformers import pipeline
|
|
| 3 |
from fastai.vision.all import *
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
|
|
|
|
|
|
| 6 |
|
| 7 |
access_token = os.getenv("access_token")
|
|
|
|
| 8 |
# NOTE - we configure docs_url to serve the interactive Docs at the root path
|
| 9 |
# of the app. This way, we can use the docs as a landing page for the app on Spaces.
|
| 10 |
app = FastAPI(docs_url="/")
|
|
@@ -13,6 +16,16 @@ pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
|
| 13 |
categories = ('Heart', 'Oblong', 'Oval', 'Round', 'Square')
|
| 14 |
learn = load_learner('model.pkl')
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
@app.get("/generate")
|
| 17 |
def generate(text: str):
|
| 18 |
"""
|
|
@@ -53,3 +66,36 @@ async def face_analyse(file: UploadFile = File(...)):
|
|
| 53 |
# Assuming categories is a list of category labels
|
| 54 |
return dict(zip(categories, map(float, probs)))
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from fastai.vision.all import *
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
| 6 |
+
import io
|
| 7 |
+
import json
|
| 8 |
|
| 9 |
access_token = os.getenv("access_token")
|
| 10 |
+
|
| 11 |
# NOTE - we configure docs_url to serve the interactive Docs at the root path
|
| 12 |
# of the app. This way, we can use the docs as a landing page for the app on Spaces.
|
| 13 |
app = FastAPI(docs_url="/")
|
|
|
|
| 16 |
categories = ('Heart', 'Oblong', 'Oval', 'Round', 'Square')
|
| 17 |
learn = load_learner('model.pkl')
|
| 18 |
|
| 19 |
+
# Initialize the Code Llama Instruct pipeline (example with 7B model)
|
| 20 |
+
llama_model_id = "meta-llama/CodeLlama-7b-Instruct-hf"
|
| 21 |
+
llama_pipeline = pipeline(
|
| 22 |
+
"text-generation",
|
| 23 |
+
model=llama_model_id,
|
| 24 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
| 25 |
+
device_map="auto",
|
| 26 |
+
use_auth_token=access_token # Use the access token for authentication
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
@app.get("/generate")
|
| 30 |
def generate(text: str):
|
| 31 |
"""
|
|
|
|
| 66 |
# Assuming categories is a list of category labels
|
| 67 |
return dict(zip(categories, map(float, probs)))
|
| 68 |
|
| 69 |
+
@app.post("/extract-frame-details")
|
| 70 |
+
def extract_frame_details(text: str):
|
| 71 |
+
"""
|
| 72 |
+
Using the Code Llama Instruct pipeline from `transformers`, extract frame
|
| 73 |
+
details from the given input text. The model used is `meta-llama/CodeLlama-7b-Instruct-hf`.
|
| 74 |
+
"""
|
| 75 |
+
messages = [
|
| 76 |
+
{"role": "system", "content": "Please provide details about frames in JSON format."},
|
| 77 |
+
{"role": "user", "content": text},
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
terminators = [
|
| 81 |
+
llama_pipeline.tokenizer.eos_token_id,
|
| 82 |
+
llama_pipeline.tokenizer.convert_tokens_to_ids("")
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
outputs = llama_pipeline(
|
| 86 |
+
messages,
|
| 87 |
+
max_new_tokens=256,
|
| 88 |
+
eos_token_id=terminators,
|
| 89 |
+
do_sample=True,
|
| 90 |
+
temperature=0.6,
|
| 91 |
+
top_p=0.9,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
generated_text = outputs[0]["generated_text"]
|
| 95 |
+
|
| 96 |
+
try:
|
| 97 |
+
extracted_info = json.loads(generated_text)
|
| 98 |
+
except json.JSONDecodeError:
|
| 99 |
+
return {"error": "Failed to parse the generated text as JSON."}
|
| 100 |
+
|
| 101 |
+
return extracted_info
|