Skip to content

Commit

Permalink
Merge branch 'master' of github.com:edenai/edenai-apis
Browse files Browse the repository at this point in the history
  • Loading branch information
floflokie committed Nov 6, 2023
2 parents f9cb3cc + 89fcf8a commit 70593c5
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 139 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ for item in ibm_res.standardized_response.items:

### Asynchronous features

If you need to use features like _speech to text_, _object extraction_ from videos, etc. Then you will have to use asynchrounous operations. This means that you will first make a call to launch an asynchrounous job, it will then return a job ID allowing you to make other calls to get the job status or response if the job is finished
If you need to use features like _speech to text_, _object extraction_ from videos, etc. Then you will have to use asynchronous operations. This means that you will first make a call to launch an asynchronous job, it will then return a job ID allowing you to make other calls to get the job status or response if the job is finished

```python
from edenai_apis import Audio
Expand Down
20 changes: 10 additions & 10 deletions edenai_apis/apis/affinda/client.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#TODO: Add tests for this file
from enum import Enum
from http import HTTPStatus
from io import BufferedReader
from json import JSONDecodeError
from typing import Any, Dict, List, Literal, Optional
from warnings import warn

import requests
from http import HTTPStatus

from edenai_apis.utils.exception import ProviderException
from edenai_apis.utils.http import HTTPMethod
from .models import Document, Organization, Workspace, Collection
from .document import DocumentState, FileParameter, QueryBuilder, UploadDocumentParams
from .models import Document, Organization, Workspace, Collection


class ExtractorType(Enum):
"""This class are use to define the type of an extractor
"""This class are used to define the type of extractor
Actually the affinda api support 6 type of extractor:
- resume
Expand Down Expand Up @@ -478,15 +478,15 @@ def create_document(
.add_workspace(self.__current_workspace)
.add_collection(self.__current_collection)
.build()
)
),
}

files: Optional[Dict[str, BufferedReader]] = None

if file.type == 'url':
payload['url'] = file.file
elif file.type == 'file':
files = { 'file': open(file.file, 'rb') }
if file.type == "url":
payload["url"] = file.file
elif file.type == "file":
files = {"file": open(file.file, "rb")}

return Document(
**self.__requests(
Expand All @@ -499,7 +499,7 @@ def create_document(
)

def delete_document(self, identifier: str) -> None:
""" Delete the document with the given identifier.
"""Delete the document with the given identifier.
Args:
identifier (str): The identifier of the document.
Expand Down
85 changes: 50 additions & 35 deletions edenai_apis/apis/alephalpha/alephalpha_api.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
from typing import Dict, Sequence, Optional

import requests
from aleph_alpha_client import (
Client,
Prompt,
SemanticEmbeddingRequest,
Image,
SemanticRepresentation,
CompletionRequest,
Text,
)

from edenai_apis.features import ProviderInterface, TextInterface, ImageInterface
from edenai_apis.features.image.embeddings import EmbeddingsDataClass, EmbeddingDataClass
from edenai_apis.features.image.embeddings import (
EmbeddingsDataClass,
EmbeddingDataClass,
)
from edenai_apis.features.image.question_answer import QuestionAnswerDataClass
from edenai_apis.features.text import SummarizeDataClass
from edenai_apis.loaders.data_loader import ProviderDataEnum
from edenai_apis.loaders.loaders import load_provider
from edenai_apis.utils.exception import ProviderException
from edenai_apis.utils.types import ResponseType

from aleph_alpha_client import Client, Prompt, SemanticEmbeddingRequest, QaRequest, Image, SemanticRepresentation, \
Document, CompletionRequest, Text


class AlephAlphaApi(ProviderInterface, TextInterface, ImageInterface):
provider_name = "alephalpha"
Expand All @@ -27,23 +36,18 @@ def __init__(self, api_keys: Dict = {}):
self.url_summarise = "https://api.aleph-alpha.com/summarize"

def text__summarize(
self,
text: str,
output_sentences: int,
language: str,
model: str,
self,
text: str,
output_sentences: int,
language: str,
model: str,
) -> ResponseType[SummarizeDataClass]:
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": f"Bearer {self.api_key}"
}
payload = {
"model": model,
"document": {
"text": text
}
"Authorization": f"Bearer {self.api_key}",
}
payload = {"model": model, "document": {"text": text}}
response = requests.post(url=self.url_summarise, headers=headers, json=payload)
if response.status_code != 200:
raise ProviderException(response.text, code=response.status_code)
Expand All @@ -57,7 +61,11 @@ def text__summarize(
)

def image__embeddings(
self, file: str, model: str, representation: str, file_url: str = "",
self,
file: str,
model: str,
representation: str,
file_url: str = "",
) -> ResponseType[EmbeddingsDataClass]:
if representation == "symmetric":
representation_client = SemanticRepresentation.Symmetric
Expand All @@ -67,47 +75,54 @@ def image__embeddings(
representation_client = SemanticRepresentation.Query
client = Client(self.api_key)
prompt = Prompt.from_image(Image.from_file(file))
request = SemanticEmbeddingRequest(prompt=prompt, representation=representation_client)
request = SemanticEmbeddingRequest(
prompt=prompt, representation=representation_client
)
try:
response = client.semantic_embed(request=request, model=model)
except:
raise ProviderException(response.message)
if response.message:
raise ProviderException(response.message)
except Exception as exc:
raise ProviderException(message=str(exc)) from exc

original_response = response._asdict()
items: Sequence[EmbeddingDataClass] = [EmbeddingDataClass(embedding=response.embedding)]
items: Sequence[EmbeddingDataClass] = [
EmbeddingDataClass(embedding=response.embedding)
]
standardized_response = EmbeddingsDataClass(items=items)
return ResponseType[EmbeddingsDataClass](
original_response=original_response,
standardized_response=standardized_response
standardized_response=standardized_response,
)

def image__question_answer(
self,
file: str,
temperature: float,
max_tokens: int,
file_url: str = "",
model: Optional[str] = None,
question: Optional[str] = None
self,
file: str,
temperature: float,
max_tokens: int,
file_url: str = "",
model: Optional[str] = None,
question: Optional[str] = None,
) -> ResponseType[QuestionAnswerDataClass]:
client = Client(self.api_key)
if question:
prompts = Prompt([Text.from_text(question), Image.from_file(file)])
else:
prompts = Prompt([Image.from_file(file)])
request = CompletionRequest(prompt=prompts, maximum_tokens=max_tokens, temperature=temperature, tokens=True)
request = CompletionRequest(
prompt=prompts,
maximum_tokens=max_tokens,
temperature=temperature,
tokens=True,
)
try:
response = client.complete(request=request, model=model)
except Exception as error:
raise ProviderException(str(error))
raise ProviderException(str(error)) from error
original_response = response._asdict()
answers = []
for answer in response.completions:
answers.append(answer.completion)
standardized_response = QuestionAnswerDataClass(answers=answers)
return ResponseType[QuestionAnswerDataClass](
original_response=original_response,
standardized_response=standardized_response
standardized_response=standardized_response,
)

2 changes: 0 additions & 2 deletions edenai_apis/apis/winstonai/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
TOKEN_BEARER = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImRkZ3NzdXRyaHpya2xsc3RnbGRiIiwicm9sZSI6ImFub24iLCJpYXQiOjE2ODY2ODc5MjMsImV4cCI6MjAwMjI2MzkyM30.bwSe1TrFMhcosgqFSlGIhMIv9fxohzLG0eyBEs7wUo8"

WINSTON_AI_API_URL = "https://api.gowinston.ai/functions/v1"
7 changes: 4 additions & 3 deletions edenai_apis/apis/winstonai/info.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"text": {
"ai_detection":{
"version" : "v1",
"version" : "v2",
"constraints": {
"languages": [
"en",
"fr",
"es",
"de"
"de",
"pt"
]
}
},
"plagia_detection":{
"version" : "v1"
"version" : "v2"
}
}
}
27 changes: 14 additions & 13 deletions edenai_apis/apis/winstonai/outputs/text/ai_detection_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,41 @@
],
"length": 2,
"credits_used": 338,
"credits_remaining": 9829
"credits_remaining": 99999662
},
"standardized_response": {
"ai_score": 0.0001,
"items": [
{
"text": "The panther, also known as the black panther, is a magnificent and enigmatic creature that captivates the imagination of many. It is not a distinct species itself, but rather a melanistic variant of leopards and jaguars. The mesmerizing black coat of the panther is a result of a genetic mutation that increases the production of dark pigment, melanin.",
"prediction": "original",
"ai_score": 0.0032
"prediction": "ai-generated",
"ai_score": 0.32
},
{
"text": "Panthers are highly adaptable predators, found primarily in dense forests and jungles across Africa, Asia, and the Americas. Their stealthy nature and exceptional agility make them formidable hunters. They are solitary creatures, preferring to roam alone in their vast territories, which can span over a hundred square miles.",
"prediction": "original",
"ai_score": 0.0003
"prediction": "ai-generated",
"ai_score": 0.03
},
{
"text": "Equipped with incredible strength and sharp retractable claws, panthers are skilled climbers and swimmers. Their keen senses, including sharp vision and acute hearing, aid them in locating prey, often stalking their victims from the cover of trees or thick underbrush before launching a precise and powerful attack. The diet of a panther consists mainly of deer, wild boar, and smaller mammals.",
"prediction": "original",
"ai_score": 0.0002
"prediction": "ai-generated",
"ai_score": 0.02
},
{
"text": "However, they are opportunistic hunters and can also target livestock and domestic animals in areas where their habitats overlap with human settlements. Unfortunately, this sometimes leads to conflicts with humans, resulting in the panther being perceived as a threat.",
"prediction": "original",
"ai_score": 0.0006
"prediction": "ai-generated",
"ai_score": 0.06
},
{
"text": "Despite their association with darkness and mystery, panthers play a vital role in maintaining the balance of ecosystems. As apex predators, they help control populations of herbivores, preventing overgrazing and maintaining healthy prey dynamics. Conservation efforts are crucial to the survival of panther populations worldwide.",
"prediction": "original",
"ai_score": 0.0001
"prediction": "ai-generated",
"ai_score": 0.01
},
{
"text": "Habitat loss, poaching, and illegal wildlife trade pose significant threats to their existence. Various organizations and governments are working tirelessly to protect these magnificent creatures through initiatives such as establishing protected areas, promoting sustainable land use practices, and raising awareness about their importance in the natural world.",
"prediction": "original",
"ai_score": 0.0001
"prediction": "ai-generated",
"ai_score": 0.01

}
]
}
Expand Down
Loading

0 comments on commit 70593c5

Please sign in to comment.