Commit
·
5ba85b9
1
Parent(s):
6c60b63
:wrench: Fix audio
Browse files
app.py
CHANGED
|
@@ -1,23 +1,21 @@
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
-
import json
|
| 4 |
import base64
|
| 5 |
import gradio as gr
|
| 6 |
import requests
|
| 7 |
-
import inspect
|
| 8 |
import pandas as pd
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
from smolagents import CodeAgent, OpenAIServerModel, DuckDuckGoSearchTool
|
| 11 |
-
import
|
| 12 |
-
|
| 13 |
-
from PIL import Image
|
| 14 |
-
import soundfile as sf
|
| 15 |
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
# (Keep Constants as is)
|
| 19 |
# --- Constants ---
|
| 20 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
# --- Basic Agent Definition ---
|
|
@@ -32,8 +30,6 @@ class BasicAgent:
|
|
| 32 |
|
| 33 |
OPENAI_MODEL_ID = os.getenv("OPENAI_MODEL_ID", "gpt-4o")
|
| 34 |
|
| 35 |
-
openai.api_key = OPENAI_API_KEY
|
| 36 |
-
|
| 37 |
self.agent = CodeAgent(
|
| 38 |
tools=[DuckDuckGoSearchTool()],
|
| 39 |
model=OpenAIServerModel(model_id=OPENAI_MODEL_ID, api_key=OPENAI_API_KEY),
|
|
@@ -83,7 +79,7 @@ def load_file_from_response(response):
|
|
| 83 |
|
| 84 |
try:
|
| 85 |
if "application/json" in content_type:
|
| 86 |
-
if "No file path" in response.json()
|
| 87 |
return None
|
| 88 |
return {"type": "json", "data": response.json()}
|
| 89 |
|
|
@@ -98,7 +94,7 @@ def load_file_from_response(response):
|
|
| 98 |
|
| 99 |
elif "audio/" in content_type:
|
| 100 |
# Transcribe audio using OpenAI Whisper
|
| 101 |
-
transcript =
|
| 102 |
model="whisper-1", file=io.BytesIO(content_bytes)
|
| 103 |
)
|
| 104 |
return {"type": "text", "data": transcript.get("text", "")}
|
|
@@ -121,7 +117,7 @@ def load_file_from_response(response):
|
|
| 121 |
# Try audio
|
| 122 |
try:
|
| 123 |
# Transcribe audio from raw bytes
|
| 124 |
-
transcript =
|
| 125 |
model="whisper-1", file=io.BytesIO(content_bytes)
|
| 126 |
)
|
| 127 |
return {"type": "text", "data": transcript.get("text", "")}
|
|
@@ -170,8 +166,8 @@ def describe_image(image_path: str) -> str:
|
|
| 170 |
],
|
| 171 |
}
|
| 172 |
]
|
| 173 |
-
response =
|
| 174 |
-
return response.choices[0].message
|
| 175 |
|
| 176 |
|
| 177 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
| 1 |
import os
|
| 2 |
import io
|
|
|
|
| 3 |
import base64
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
|
|
|
| 6 |
import pandas as pd
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from smolagents import CodeAgent, OpenAIServerModel, DuckDuckGoSearchTool
|
| 9 |
+
from openai import OpenAI
|
| 10 |
+
|
| 11 |
+
from PIL import Image, UnidentifiedImageError
|
|
|
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
# (Keep Constants as is)
|
| 16 |
# --- Constants ---
|
| 17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 19 |
|
| 20 |
|
| 21 |
# --- Basic Agent Definition ---
|
|
|
|
| 30 |
|
| 31 |
OPENAI_MODEL_ID = os.getenv("OPENAI_MODEL_ID", "gpt-4o")
|
| 32 |
|
|
|
|
|
|
|
| 33 |
self.agent = CodeAgent(
|
| 34 |
tools=[DuckDuckGoSearchTool()],
|
| 35 |
model=OpenAIServerModel(model_id=OPENAI_MODEL_ID, api_key=OPENAI_API_KEY),
|
|
|
|
| 79 |
|
| 80 |
try:
|
| 81 |
if "application/json" in content_type:
|
| 82 |
+
if "No file path" in response.json().detail:
|
| 83 |
return None
|
| 84 |
return {"type": "json", "data": response.json()}
|
| 85 |
|
|
|
|
| 94 |
|
| 95 |
elif "audio/" in content_type:
|
| 96 |
# Transcribe audio using OpenAI Whisper
|
| 97 |
+
transcript = client.audio.transcriptions.create(
|
| 98 |
model="whisper-1", file=io.BytesIO(content_bytes)
|
| 99 |
)
|
| 100 |
return {"type": "text", "data": transcript.get("text", "")}
|
|
|
|
| 117 |
# Try audio
|
| 118 |
try:
|
| 119 |
# Transcribe audio from raw bytes
|
| 120 |
+
transcript = client.audio.transcriptions.create(
|
| 121 |
model="whisper-1", file=io.BytesIO(content_bytes)
|
| 122 |
)
|
| 123 |
return {"type": "text", "data": transcript.get("text", "")}
|
|
|
|
| 166 |
],
|
| 167 |
}
|
| 168 |
]
|
| 169 |
+
response = client.chat.completions.create(model="gpt-4o", messages=messages)
|
| 170 |
+
return response.choices[0].message.content
|
| 171 |
|
| 172 |
|
| 173 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|