Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add llava generate #4

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lmm_tools/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BASETEN_API_KEY = "PRxjuebe.VQJQ7rCvswimP5y8GeSmZA03I4zw6dgB"
BASETEN_URL = "https://model-232pg41q.api.baseten.co/production/predict"
18 changes: 14 additions & 4 deletions lmm_tools/lmm/lmm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import base64
import requests
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, Dict, List, Optional, Union, cast
from lmm_tools.config import BASETEN_API_KEY, BASETEN_URL


def encode_image(image: Union[str, Path]) -> str:
Expand All @@ -12,7 +14,7 @@ def encode_image(image: Union[str, Path]) -> str:

class LMM(ABC):
@abstractmethod
def generate(self, prompt: str, image: Optional[Union[str, Path]]) -> str:
def generate(self, prompt: str, image: Optional[Union[str, Path]] = None) -> str:
pass


Expand All @@ -22,8 +24,16 @@ class LLaVALMM(LMM):
def __init__(self, name: str):
self.name = name

def generate(self, prompt: str, image: Optional[Union[str, Path]]) -> str:
raise NotImplementedError("LLaVA LMM not implemented yet")
def generate(self, prompt: str, image: Optional[Union[str, Path]] = None) -> str:
data = {"prompt": prompt}
if image:
data["image"] = encode_image(image)
res = requests.post(
BASETEN_URL,
headers={"Authorization": f"Api-Key {BASETEN_API_KEY}"},
json=data,
)
return res.text


class OpenAILMM(LMM):
Expand All @@ -35,7 +45,7 @@ def __init__(self, name: str):
self.name = name
self.client = OpenAI()

def generate(self, prompt: str, image: Optional[Union[str, Path]]) -> str:
def generate(self, prompt: str, image: Optional[Union[str, Path]] = None) -> str:
message: List[Dict[str, Any]] = [
{
"role": "user",
Expand Down
Loading