Spaces:
Sleeping
Sleeping
added /frame-details (#1)
Browse files- added /frame-details (8119b22648e35787a42fb20aaefe7ead6d118342)
Co-authored-by: Funke <[email protected]>
app.py
CHANGED
|
@@ -49,4 +49,55 @@ async def face_analyse(file: UploadFile = File(...)):
|
|
| 49 |
return {"error": "Failed to make predictions."}
|
| 50 |
|
| 51 |
# Assuming categories is a list of category labels
|
| 52 |
-
return dict(zip(categories, map(float, probs)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return {"error": "Failed to make predictions."}
|
| 50 |
|
| 51 |
# Assuming categories is a list of category labels
|
| 52 |
+
return dict(zip(categories, map(float, probs)))
|
| 53 |
+
|
| 54 |
+
# Initialize the Meta-Llama-3-70B-Instruct pipeline
|
| 55 |
+
llama_model_id = "meta-llama/Meta-Llama-3-70B-Instruct"
|
| 56 |
+
llama_pipeline = pipeline(
|
| 57 |
+
"text-generation",
|
| 58 |
+
model=llama_model_id,
|
| 59 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
| 60 |
+
device_map="auto",
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
@app.post("/frame-details")
|
| 64 |
+
def frame_details(text: str):
|
| 65 |
+
"""
|
| 66 |
+
Extract structured information from a given text about frames using the
|
| 67 |
+
Meta-Llama-3-70B-Instruct model. The model will output details like price, color, etc.
|
| 68 |
+
"""
|
| 69 |
+
messages = [
|
| 70 |
+
{"role": "system", "content": "You are an api chatbot for frames and glasses who always responds with only a json. Extract the infomation given into a structured json for frame details"},
|
| 71 |
+
{"role": "user", "content": text},
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
terminators = [
|
| 75 |
+
llama_pipeline.tokenizer.eos_token_id,
|
| 76 |
+
llama_pipeline.tokenizer.convert_tokens_to_ids("")
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
outputs = llama_pipeline(
|
| 80 |
+
messages,
|
| 81 |
+
max_new_tokens=256,
|
| 82 |
+
eos_token_id=terminators,
|
| 83 |
+
do_sample=True,
|
| 84 |
+
temperature=0.6,
|
| 85 |
+
top_p=0.9,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Extract the last generated text from the output
|
| 89 |
+
generated_text = outputs[0]["generated_text"]
|
| 90 |
+
|
| 91 |
+
# Parse the generated text to extract structured information (this is an example and should be customized)
|
| 92 |
+
# Here, you would add your own logic to parse the generated text
|
| 93 |
+
# For now, we'll assume the generated text is in JSON format
|
| 94 |
+
try:
|
| 95 |
+
extracted_info = eval(generated_text) # It's recommended to use `json.loads` in a real application
|
| 96 |
+
except Exception as e:
|
| 97 |
+
return {"error": "Failed to parse the generated text."}
|
| 98 |
+
|
| 99 |
+
return extracted_info
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
import uvicorn
|
| 103 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|