Alwrity - Bug fixes

This commit is contained in:
AjaySi
2024-04-12 17:36:37 +05:30
parent cf6516eeee
commit e3c3c03729
18 changed files with 81 additions and 81 deletions

View File

@@ -79,7 +79,7 @@ def speech_to_text(video_url, output_path='.'):
logger.info("Transcribing using OpenAI's Whisper model.")
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=open(audio_file, "rb"),
file=open(audio_file, "rb", encoding="utf-8"),
response_format="text"
)
logger.info(f"\nYouTube video transcription:\n{yt.title}\n{transcript}\n")
@@ -133,7 +133,7 @@ def long_video(temp_file_name):
for i, chunk in enumerate(chunks):
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as audio_chunk_file:
chunk.write_audiofile(audio_chunk_file.name, codec="mp3")
with open(audio_chunk_file.name, "rb") as audio_file:
with open(audio_chunk_file.name, "rb", encoding="utf-8") as audio_file:
# Transcribe each chunk using OpenAI's Whisper API
app.logger.info(f"Transcribing chunk {i+1}/{len(chunks)}")
transcript = openai.Audio.transcribe("whisper-1", audio_file)

View File

@@ -35,7 +35,7 @@ def gen_new_from_given_img(img_path, image_dir, num_img=1, img_size="1024x1024",
client = OpenAI()
variation_response = client.images.create_variation(
image=open(img_path, "rb"),
image=open(img_path, "rb", encoding="utf-8"),
n=num_img,
size=img_size,
response_format=response_format

View File

@@ -32,7 +32,7 @@ def analyze_and_extract_details_from_image(image_path):
def encode_image(path):
""" Encodes an image to a base64 string. """
with open(path, "rb") as image_file:
with open(path, "rb", encoding="utf-8") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
base64_image = encode_image(image_path)

View File

@@ -4,6 +4,7 @@ import sys
from pathlib import Path
import google.generativeai as genai
from google.api_core import retry
from dotenv import load_dotenv
load_dotenv(Path('../../../.env'))
from loguru import logger
@@ -13,16 +14,10 @@ logger.add(sys.stdout,
format="<level>{level}</level>|<green>{file}:{line}:{function}</green>| {message}"
)
from tenacity import (
retry,
stop_after_attempt,
wait_random_exponential,
) # for exponential backoff
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
def gemini_text_response(prompt, temperature, top_p, n, max_tokens):
""" Common functiont to get response from gemini pro Text. """
#FIXME: Include : https://github.com/google-gemini/cookbook/blob/main/quickstarts/rest/System_instructions_REST.ipynb
try:
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
except Exception as err:
@@ -35,10 +30,13 @@ def gemini_text_response(prompt, temperature, top_p, n, max_tokens):
"top_k": n,
"max_output_tokens": max_tokens
}
# FIXME: Expose model_name in main_config
model = genai.GenerativeModel(model_name="gemini-1.0-pro", generation_config=generation_config)
try:
response = model.generate_content(prompt, stream=True)
# text_response = []
response = model.generate_content(prompt, stream=True, request_options={'retry':retry.Retry()})
for chunk in response:
# text_response.append(chunk.text)
print(chunk.text)
return response.text
except Exception as err: