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 two streamlit demos. #268

Merged
merged 3 commits into from
Apr 21, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[theme]
base="dark"
21 changes: 21 additions & 0 deletions docs/bootcamp/streamlit/gptcache-streamlit-audio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# GPTCache Demo for OpenAI Audio Transcription
This project demonstrates how [GPTcache](https://github.com/zilliztech/GPTCache) can be used to save costs when using OpenAI’s audio transcription API. It provides a simple Streamlit app that allows users to input an audio file and see the corresponding transcribed text. The app uses a cache to store previously generated transcriptions and reuses them for the same audio file, thus avoiding making duplicate API calls.

## Requirements
* Python 3.6 or later
* Dependencies listed in requirements.txt
* OpenAI API key
## Usage
1. Clone the repository to your local machine
Install the required packages: pip install -r requirements.txt
2. Run the app: streamlit run audio.py
3. Open the app in your browser at http://localhost:8501
4. Enter your OpenAI API key and upload an audio file to transcribe, then click “generate” to wait for the transcribed text to appear.
If a cache hit occurred, you should see a message like “cache” at the bottom of the transcribed text.

<p align="center">
<img src="./example.png" alt="example"/>
</p>



60 changes: 60 additions & 0 deletions docs/bootcamp/streamlit/gptcache-streamlit-audio/audio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import streamlit as st
import os
import uuid

from gptcache import cache
from gptcache.manager import get_data_manager, CacheBase, VectorBase, ObjectBase
from gptcache.adapter import openai
from gptcache.processor.pre import get_file_name
from gptcache.embedding import Data2VecAudio
from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation


@st.cache_resource
def initialize_configuration():
data2vec = Data2VecAudio()
data_manager = get_data_manager(CacheBase('sqlite', sql_url='sqlite:///./local/gptcache20.db'),
VectorBase('faiss', dimension=data2vec.dimension, index_path='./local/faiss20.index'),
ObjectBase('local', path='./local'))
cache.init(
pre_embedding_func=get_file_name,
embedding_func=data2vec.to_embeddings,
data_manager=data_manager,
similarity_evaluation=SearchDistanceEvaluation(),
)
return data_manager

data_manager = initialize_configuration()

def api_call(audio_bytes, open_ai_key):
os.environ['OPENAI_API_KEY'] = open_ai_key
os.environ['CURL_CA_BUNDLE'] = ''
os.environ['KMP_DUPLICATE_LIB_OK']='TRUE'
cache.set_openai_key()
transcript = openai.Audio.transcribe('whisper-1', audio_bytes, api_key=open_ai_key)
is_cached = transcript.get('gptcache', False)
return transcript['text'], is_cached

def main():

st.title('GPTCache for Audio Demo')
open_ai_key = st.text_input('OpenAI key')
audio_file = st.file_uploader('Choose an audio file (.mp3, .wav, or .ogg)', type=['mp3', 'wav', 'ogg'])

if st.button('generate', key='button'):
file_extension = os.path.splitext(audio_file.name)[1] # Get the extension of the uploaded file
random_filename = str(uuid.uuid4()) + file_extension
with open(random_filename, 'wb') as f:
f.write(audio_file.getbuffer())
audio_file_handler = open(random_filename, 'rb')
text, is_cached = api_call(audio_file_handler, open_ai_key)
st.write(text)
os.remove(random_filename)

if is_cached:
st.markdown('<div style="display: flex; align-items: center; justify-content: center; \
background-color: green; padding: 10px; color: white; font-weight: bold; border-radius: \
5px; margin: 10px auto; max-width: 100px;">cache</div>', unsafe_allow_html=True)

if __name__ == '__main__':
main()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
gptcache
pillow
streamlit
torch
faiss-cpu
torchaudio
transformers
sqlalchemy
22 changes: 22 additions & 0 deletions docs/bootcamp/streamlit/gptcache-streamlit-image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# GPTCache Demo for OpenAI Image Generation
This project demonstrates how [GPTcache](https://github.com/zilliztech/GPTCache) can be used to save costs when using OpenAI’s DALL-E API. It provides a simple Streamlit app that allows users to input a prompt and see the corresponding DALL-E output image. The app uses a cache to store previously generated images and reuses them for the same prompt, thus avoiding making duplicate API calls. There is an online [demo](https://gptcache-openai-image.streamlit.app/) hosted for preview.

## Requirements
* Python 3.6 or later
* Dependencies listed in requirements.txt
* OpenAI API key
## Usage
1. Clone the repository to your local machine
Install the required packages: pip install -r requirements.txt
2. Run the app: streamlit run imagen.py
3. Open the app in your browser at http://localhost:8501
4. Enter your OpenAI key and prompt then click “generate” to
wait for the DALL-E output image to appear.
If a cache hit occurred, you should see a message like “cache” at the bottom of the image.

<p align="center">
<img src="./example.png" alt="example"/>
</p>



Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 81 additions & 0 deletions docs/bootcamp/streamlit/gptcache-streamlit-image/imagen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import streamlit as st
from PIL import Image
import os
import io
import base64
from io import BytesIO
import requests

from gptcache import cache
from gptcache.manager import get_data_manager, CacheBase, VectorBase, ObjectBase
from gptcache.adapter import openai
from gptcache.processor.pre import get_prompt
from gptcache.embedding import Onnx
from gptcache.similarity_evaluation import ExactMatchEvaluation

st.title('GPTCache for Image Demo')

@st.cache_resource
def initialize_configuration():
onnx = Onnx()
data_manager = get_data_manager(CacheBase('sqlite', sql_url='sqlite:///./local/gptcache10.db'),
VectorBase('faiss', dimension=onnx.dimension, index_path='./local/faiss10.index'),
ObjectBase('local', path='./local'))
cache.init(
pre_embedding_func=get_prompt,
embedding_func=onnx.to_embeddings,
data_manager=data_manager,
similarity_evaluation=ExactMatchEvaluation(),
)
return data_manager

data_manager = initialize_configuration()

def api_call(text_input, open_ai_key):
os.environ['CURL_CA_BUNDLE'] = ''
response = openai.Image.create(
prompt=text_input,
n=1,
size='256x256',
api_key=open_ai_key
)
image_url = response['data'][0]['url']

is_cached = response.get('gptcache', False)
if is_cached is False:
response = requests.get(image_url)
img = Image.open(BytesIO(response.content))
else:
img = Image.open(image_url)
return img, is_cached

def main():
open_ai_key = st.text_input('OpenAI key:')
text_input = st.text_input('prompt:')

if st.button('generate', key='button'):
try:
image, is_cached = api_call(text_input, open_ai_key)
width, height = image.size
desired_width = 500
desired_height = int(height * desired_width / width)
resized_image = image.resize((desired_width, desired_height))
img_bytes = io.BytesIO()
resized_image.save(img_bytes, format='PNG')
img_str = base64.b64encode(img_bytes.getvalue()).decode()

st.markdown(
f'<div style="display: flex; justify-content: center;"><img src="data:image/png;base64,{img_str}" \
alt="Uploaded Image" width="{desired_width}"></div>',
unsafe_allow_html=True
)

if is_cached:
st.markdown('<div style="display: flex; align-items: center; justify-content: center; background-color: \
green; padding: 10px; color: white; font-weight: bold; border-radius: 5px; margin: 10px auto; \
max-width: 100px;">cache</div>', unsafe_allow_html=True)
except Exception as e:
st.error('invalid OpenAI API key or inappropriate prompt rejected by OpenAI.')

if __name__ == '__main__':
main()
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
gptcache
pillow
streamlit
onnxruntime
faiss-cpu
transformers
sqlalchemy