Spaces:
Running
Running
sachin
commited on
Commit
·
510d2c1
1
Parent(s):
1b719ac
add pixtral
Browse files- recipes/urls.py +2 -0
- recipes/views.py +51 -0
recipes/urls.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
from django.urls import path
|
2 |
from .views import recipe_generate_route, execute_prompt_route_get
|
|
|
3 |
|
4 |
urlpatterns = [
|
5 |
path('execute_prompt_get/', execute_prompt_route_get, name='execute_prompt_get'),
|
6 |
path('recipe_generate/', recipe_generate_route, name='recipe_generate'),
|
|
|
7 |
]
|
|
|
1 |
from django.urls import path
|
2 |
from .views import recipe_generate_route, execute_prompt_route_get
|
3 |
+
from .views import VisionLLMView
|
4 |
|
5 |
urlpatterns = [
|
6 |
path('execute_prompt_get/', execute_prompt_route_get, name='execute_prompt_get'),
|
7 |
path('recipe_generate/', recipe_generate_route, name='recipe_generate'),
|
8 |
+
path('vision_llm_url/', VisionLLMView.as_view()),
|
9 |
]
|
recipes/views.py
CHANGED
@@ -3,6 +3,10 @@ from rest_framework.decorators import api_view
|
|
3 |
from rest_framework.response import Response
|
4 |
from rest_framework import serializers
|
5 |
from .engine import execute_prompt, bundle_function, propose_recipes, compute_reduced_prices
|
|
|
|
|
|
|
|
|
6 |
import json
|
7 |
|
8 |
class PromptSerializer(serializers.Serializer):
|
@@ -34,3 +38,50 @@ def recipe_generate_route(request):
|
|
34 |
return Response({'error': 'Something went wrong'}, status=500)
|
35 |
return Response(result)
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from rest_framework.response import Response
|
4 |
from rest_framework import serializers
|
5 |
from .engine import execute_prompt, bundle_function, propose_recipes, compute_reduced_prices
|
6 |
+
from rest_framework.views import APIView
|
7 |
+
from mistralai import Mistral
|
8 |
+
import os
|
9 |
+
import base64
|
10 |
import json
|
11 |
|
12 |
class PromptSerializer(serializers.Serializer):
|
|
|
38 |
return Response({'error': 'Something went wrong'}, status=500)
|
39 |
return Response(result)
|
40 |
|
41 |
+
|
42 |
+
|
43 |
+
class VisionLLMView(APIView):
|
44 |
+
def post(self, request, format=None):
|
45 |
+
data = request.data
|
46 |
+
|
47 |
+
#print(data)
|
48 |
+
# Retrieve the API key from environment variables
|
49 |
+
api_key = os.environ["MISTRAL_API_KEY"]
|
50 |
+
|
51 |
+
# Specify model
|
52 |
+
model = "pixtral-12b-2409"
|
53 |
+
|
54 |
+
# Initialize the Mistral client
|
55 |
+
client = Mistral(api_key=api_key)
|
56 |
+
|
57 |
+
# Decode the base64 image
|
58 |
+
#image_data = base64.b64decode(data['image'])
|
59 |
+
#image_data = base64.b64decode(data['messages'][0]['image'][0])
|
60 |
+
image_data = (data['messages'][0]['image'][0])
|
61 |
+
|
62 |
+
# Define the messages for the chat
|
63 |
+
messages = [
|
64 |
+
{
|
65 |
+
"role": "user",
|
66 |
+
"content": [
|
67 |
+
{
|
68 |
+
"type": "text",
|
69 |
+
"text": data['messages'][0]['prompt']
|
70 |
+
},
|
71 |
+
{
|
72 |
+
"type": "image_url",
|
73 |
+
"image_url": f"data:image/jpeg;base64,{image_data}"
|
74 |
+
}
|
75 |
+
]
|
76 |
+
}
|
77 |
+
]
|
78 |
+
|
79 |
+
# Get the chat response
|
80 |
+
chat_response = client.chat.complete(
|
81 |
+
model=model,
|
82 |
+
messages=messages
|
83 |
+
)
|
84 |
+
#print(chat_response.choices[0].message.content)
|
85 |
+
# Return the content of the response
|
86 |
+
return Response({"response": chat_response.choices[0].message.content})
|
87 |
+
|