Metadata-Version: 2.1
Name: sjgeminifvai
Version: 1.0.0
Summary: Access Gemini Android SDK in Python
Author: Kenechukwu Akubue
Author-email: kengoon19@gmail.com
Requires-Python: >=3.8,<4.0
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Dist: pyjnius (>=1.6.1,<2.0.0)
Requires-Dist: simplejnius (>=0.1.3,<0.2.0)
Description-Content-Type: text/markdown

# Gemini (Firebase-Vertex-AI)

Access Gemini Android SDK in Python

> [!CAUTION]
> Firebase-Vertex-AI is now deprecated by the firebase team. Firebase Vertex AI is now Firebase AI.
> You can find the new Python SimpleJnius implementation [here](https://github.com/SimpleJnius/sj-firebase-ai)

<!-- GitAds-Verify: 9U81VHQ4LFABYVNK6Q585J18H96UMN51 -->
## GitAds Sponsored
[![Sponsored by GitAds](https://gitads.dev/v1/ad-serve?source=simplejnius/sj-gemini-vertex-ai@github)](https://gitads.dev/v1/ad-track?source=simplejnius/sj-gemini-vertex-ai@github)



## Usage
**Note: NO NEED FOR THREAD OR ASYNC.**

### For autocompletion in IDE
```shell
pip install sjgeminifvai
```

### Set up your firebase project for Android
Read more [here](https://firebase.google.com/docs/vertex-ai/get-started?hl=en&authuser=0&platform=android#set-up-firebase)

### Add SDK to buildozer.spec file
```rpmspec
requirements=sjgeminifvai,simplejnius

android.gradle_dependencies=com.google.guava:guava:32.0.1-android,
  org.reactivestreams:reactive-streams:1.0.4,com.google.firebase:firebase-vertexai:16.0.0-beta04
```

### Interact with Vertex Gemini API Without Streaming
Wait for the entire result instead of streaming; 
the result is only returned after the model completes the entire generation process.

```python
from sjgeminifvai.jclass import (
    FirebaseVertexAI,
    ContentBuilder,
    GenerativeModelFutures
)
from simplejnius.guava.jclass import Futures
from simplejnius.guava.jinterface import FutureCallback
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput


class GeminiApp(App):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.future_callback = None
        self.response = None
        self.prompt = None
        self.textinput = None
        self.label = None

        # Initialize the Vertex AI service and the generative model
        # Specify a model that supports your use case
        # Gemini 1.5 models are versatile and can be used with all API capabilities
        vertex = FirebaseVertexAI.getInstance()
        self.gm = vertex.generativeModel("gemini-1.5-flash")

        # Use the GenerativeModelFutures Java compatibility layer which offers
        # support for ListenableFuture and Publisher APIs
        self.model = GenerativeModelFutures.from_(self.gm)

    def build(self):
        self.label = Label()
        self.textinput = TextInput(
            size_hint_y=.1,
            hint_text="Chat with gemini",
            on_text_validate=self.chat_gemini
        )
        box = BoxLayout(orientation="vertical")
        box.add_widget(self.label)
        box.add_widget(self.textinput)
        return box

    def chat_gemini(self, instance):
        # Provide a prompt that contains text
        self.prompt = (
            ContentBuilder()
            .addText(instance.text)
            .build()
        )

        # To generate text output, call generateContent with the text input
        self.response = self.model.generateContentResponse(self.prompt)

        self.future_callback = FutureCallback(
            callback=dict(
                on_success=self.get_gemin_reply,
                on_failure=print
            )
        )
        Futures.addCallback(self.response, self.future_callback)

    def get_gemini_reply(self, result):
        self.label.text = result.getText()


if __name__ == "__main__":
    GeminiApp().run()

# report any bug or error if the above code does not work as expected
```

### Interact with Vertex Gemini API With Streaming
You can achieve faster interactions by not waiting for the entire result from the model generation, 
and instead use streaming to handle partial results.

This example shows how to use generateContentStream to stream 
generated text from a prompt request that includes only text:

```python
from sjgeminifvai.jclass import (
    FirebaseVertexAI,
    ContentBuilder,
    GenerativeModelFutures
)
from simplejnius.reactivestreams.jinterface import Subscriber
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.clock import Clock


class GeminiApp(App):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.subscriber = None
        self.gcr = None
        self.streaming_response = None
        self.prompt = None
        self.textinput = None
        self.label = None

        # Initialize the Vertex AI service and the generative model
        # Specify a model that supports your use case
        # Gemini 1.5 models are versatile and can be used with all API capabilities
        vertex = FirebaseVertexAI.getInstance()
        self.gm = vertex.generativeModel("gemini-1.5-flash")

        # Use the GenerativeModelFutures Java compatibility layer which offers
        # support for ListenableFuture and Publisher APIs
        self.model = GenerativeModelFutures.from_(self.gm)

    def build(self):
        self.label = Label()
        self.textinput = TextInput(
            size_hint_y=.1,
            hint_text="Chat with gemini",
            on_text_validate=self.chat_gemini
        )
        box = BoxLayout(orientation="vertical")
        box.add_widget(self.label)
        box.add_widget(self.textinput)
        return box

    def chat_gemini(self, instance):
        # Provide a prompt that contains text
        self.prompt = (
            ContentBuilder()
            .addText(instance.text)
            .build()
        )

        # To stream generated text output, call generateContentStream with the text input
        self.streaming_response = self.model.generateContentStream(self.prompt)

        self.subscriber = Subscriber(
            callback=dict(
                on_next=self.get_gemini_reply,
                on_complete=lambda result: setattr(self.label, "text", result.getText()),
                on_error=print,
                on_subscribe=print
            )
        )
        self.streaming_response.subscribe(self.subscriber)

    def get_gemini_reply(self, result):
        chunk = result.getText()

        def add_chunk_to_label(_):
            self.label.text += chunk

        Clock.schedule_once(add_chunk_to_label)


if __name__ == "__main__":
    GeminiApp().run()

# report any bug or error if the above code does not work as expected
```

# Examples to interact with images and videos coming soon 

