Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,11 @@ from scipy.io.wavfile import write
|
|
| 12 |
import PIL
|
| 13 |
from openai import OpenAI
|
| 14 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
dotenv.load_dotenv()
|
| 16 |
|
| 17 |
seamless_client = Client("facebook/seamless_m4t")
|
|
@@ -81,32 +86,91 @@ def process_speech(input_language, audio_input):
|
|
| 81 |
except Exception as e :
|
| 82 |
return f"{e}"
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
-
def
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
payload = {
|
| 94 |
-
"
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
],
|
| 98 |
-
"
|
| 99 |
}
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
| 110 |
|
| 111 |
|
| 112 |
def query_vectara(text):
|
|
|
|
| 12 |
import PIL
|
| 13 |
from openai import OpenAI
|
| 14 |
import time
|
| 15 |
+
from PIL import Image
|
| 16 |
+
import io
|
| 17 |
+
import hashlib
|
| 18 |
+
import datetime
|
| 19 |
+
|
| 20 |
dotenv.load_dotenv()
|
| 21 |
|
| 22 |
seamless_client = Client("facebook/seamless_m4t")
|
|
|
|
| 86 |
except Exception as e :
|
| 87 |
return f"{e}"
|
| 88 |
|
| 89 |
+
def decode_image(encoded_image: str) -> Image:
|
| 90 |
+
decoded_bytes = base64.b64decode(encoded_image.encode("utf-8"))
|
| 91 |
+
buffer = io.BytesIO(decoded_bytes)
|
| 92 |
+
image = Image.open(buffer)
|
| 93 |
+
return image
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def encode_image(image: Image.Image, format: str = "PNG") -> str:
|
| 97 |
+
with io.BytesIO() as buffer:
|
| 98 |
+
image.save(buffer, format=format)
|
| 99 |
+
encoded_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 100 |
+
return encoded_image
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def get_conv_log_filename():
|
| 104 |
+
t = datetime.datetime.now()
|
| 105 |
+
name = os.path.join(LOGDIR, f"{t.year}-{t.month:02d}-{t.day:02d}-conv.json")
|
| 106 |
+
return name
|
| 107 |
+
|
| 108 |
|
| 109 |
+
def get_conv_image_dir():
|
| 110 |
+
name = os.path.join(LOGDIR, "images")
|
| 111 |
+
os.makedirs(name, exist_ok=True)
|
| 112 |
+
return name
|
| 113 |
|
| 114 |
|
| 115 |
+
def get_image_name(image, image_dir=None):
|
| 116 |
+
buffer = io.BytesIO()
|
| 117 |
+
image.save(buffer, format="PNG")
|
| 118 |
+
image_bytes = buffer.getvalue()
|
| 119 |
+
md5 = hashlib.md5(image_bytes).hexdigest()
|
| 120 |
+
|
| 121 |
+
if image_dir is not None:
|
| 122 |
+
image_name = os.path.join(image_dir, md5 + ".png")
|
| 123 |
+
else:
|
| 124 |
+
image_name = md5 + ".png"
|
| 125 |
+
|
| 126 |
+
return image_name
|
| 127 |
+
|
| 128 |
+
def resize_image(image, max_size):
|
| 129 |
+
width, height = image.size
|
| 130 |
+
aspect_ratio = float(width) / float(height)
|
| 131 |
+
|
| 132 |
+
if width > height:
|
| 133 |
+
new_width = max_size
|
| 134 |
+
new_height = int(new_width / aspect_ratio)
|
| 135 |
+
else:
|
| 136 |
+
new_height = max_size
|
| 137 |
+
new_width = int(new_height * aspect_ratio)
|
| 138 |
|
| 139 |
+
resized_image = image.resize((new_width, new_height))
|
| 140 |
+
return resized_image
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def process_image(image_input, text_input):
|
| 145 |
+
# Resize the image if needed
|
| 146 |
+
max_image_size = 1024 # You can adjust this size
|
| 147 |
+
image = resize_image(image_input, max_image_size)
|
| 148 |
+
|
| 149 |
+
# Encode the image to base64
|
| 150 |
+
base64_image_str = encode_image(image)
|
| 151 |
+
|
| 152 |
+
# Prepare the payload for the HTTP request
|
| 153 |
payload = {
|
| 154 |
+
"content": [
|
| 155 |
+
{
|
| 156 |
+
"prompt": text_input,
|
| 157 |
+
"image": base64_image_str,
|
| 158 |
+
}
|
| 159 |
],
|
| 160 |
+
"token": "sk-OtterHD", # Replace with your actual token
|
| 161 |
}
|
| 162 |
|
| 163 |
+
# Specify the URL for the HTTP request
|
| 164 |
+
url = "https://ensures-picture-choices-labels.trycloudflare.com/app/otter"
|
| 165 |
+
headers = {"Content-Type": "application/json"}
|
| 166 |
|
| 167 |
+
# Make the HTTP request
|
| 168 |
+
response = requests.post(url, headers=headers, data=json.dumps(payload))
|
| 169 |
+
if response.status_code == 200:
|
| 170 |
+
results = response.json()
|
| 171 |
+
return results["result"]
|
| 172 |
+
else:
|
| 173 |
+
return f"Error: {response.status_code}, {response.text}"
|
| 174 |
|
| 175 |
|
| 176 |
def query_vectara(text):
|