Spaces:
Sleeping
Sleeping
import json | |
import os | |
import requests | |
def get_transcript_for_url(url: str) -> dict: | |
""" | |
This function fetches the transcript data for a signed URL. | |
If the URL results in a direct download, it processes the downloaded content. | |
:param url: Signed URL for the JSON file | |
:return: Parsed JSON data as a dictionary | |
""" | |
headers = { | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" | |
} | |
try: | |
response = requests.get(url, headers=headers) | |
response.raise_for_status() | |
if "application/json" in response.headers.get("Content-Type", ""): | |
return response.json() # Parse and return JSON directly | |
# If not JSON, assume it's a file download (e.g., content-disposition header) | |
content_disposition = response.headers.get("Content-Disposition", "") | |
if "attachment" in content_disposition: | |
# Process the content as JSON | |
return json.loads(response.content) | |
return json.loads(response.content) | |
except requests.exceptions.HTTPError as http_err: | |
print(f"HTTP error occurred: {http_err}") | |
except requests.exceptions.RequestException as req_err: | |
print(f"Request error occurred: {req_err}") | |
except json.JSONDecodeError as json_err: | |
print(f"JSON decoding error: {json_err}") | |
return {} | |
def setup_openai_key() -> None: | |
"""Set up OpenAI API key from file.""" | |
try: | |
with open("api.key", "r") as f: | |
os.environ["OPENAI_API_KEY"] = f.read().strip() | |
except FileNotFoundError: | |
print("Using ENV variable") | |
openai_tools = [ | |
{ | |
"type": "function", | |
"function": { | |
"name": "correct_speaker_name_with_url", | |
"description": "If a User provides a link to Agenda file, call the correct_speaker_name_with_url function to correct the speaker names based on the url, i.e if a user says 'Here is the Luma link for the event' and provides a link to the event, the function will correct the speaker names based on the event.", | |
"parameters": { | |
"type": "object", | |
"properties": { | |
"url": {"type": "string", "description": "The url to the agenda."} | |
}, | |
"required": ["url"], | |
"additionalProperties": False, | |
}, | |
}, | |
}, | |
{ | |
"type": "function", | |
"function": { | |
"name": "correct_call_type", | |
"description": "If the user tells you the correct call type, you have to apologize and call this function with correct call type.", | |
"parameters": { | |
"type": "object", | |
"properties": { | |
"call_type": { | |
"type": "string", | |
"description": "The correct call type. If street interview, call type is 'si'.", | |
} | |
}, | |
"required": ["call_type"], | |
"additionalProperties": False, | |
}, | |
}, | |
}, | |
{ | |
"type": "function", | |
"function": { | |
"name": "get_image", | |
"description": "If the user asks you to show crops, you need to call this function", | |
"parameters": { | |
"type": "object", | |
"properties": {}, | |
"required": [], | |
"additionalProperties": False, | |
}, | |
}, | |
}, | |
] | |
css = """ | |
.gradio-container { | |
padding-top: 0px !important; | |
padding-left: 0px !important; | |
padding-right: 0px !important; | |
padding: 0px !important; | |
margin: 0px !important; | |
} | |
#component-0 { | |
gap: 0px !important; | |
} | |
.icon-button-wrapper button[title="Clear"] { | |
display: none !important; | |
} | |
.image-preview .icon-button-wrapper { | |
display: block !important; | |
} | |
.image-preview .icon-button-wrapper button[title="Clear"] { | |
display: block !important; | |
} | |
.download-link { | |
display: none !important; | |
} | |
footer { | |
display: none !important; | |
} | |
#chatbot_box{ | |
flex-grow: 1 !important; | |
border-width: 0px !important; | |
} | |
#link-frame { | |
position: absolute !important; | |
width: 1px !important; | |
height: 1px !important; | |
right: -100px !important; | |
bottom: -100px !important; | |
display: none !important; | |
} | |
.html-container { | |
display: none !important; | |
} | |
a { | |
text-decoration: none !important; | |
} | |
#topic { | |
color: #999 !important; | |
} | |
.bubble-wrap { | |
padding-top: 0px !important; | |
} | |
.message-content { | |
border: 0px !important; | |
margin: 5px !important; | |
} | |
.message-row { | |
border-style: none !important; | |
margin: 0px !important; | |
width: 100% !important; | |
max-width: 100% !important; | |
} | |
.flex-wrap { | |
border-style: none !important; | |
} | |
.panel-full-width { | |
border-style: none !important; | |
border-width: 0px !important; | |
} | |
ol { | |
list-style-position: outside; | |
margin-left: 20px; | |
} | |
body.waiting * { | |
cursor: progress; | |
} | |
""" | |
head = f""" | |
<script defer src="https://www.gstatic.com/firebasejs/11.1.0/firebase-firestore.js"></script> | |
<script type="module"> | |
// Import the functions you need from the SDKs you need | |
import {{ initializeApp }} from "https://www.gstatic.com/firebasejs/11.1.0/firebase-app.js"; | |
import {{ getDatabase, ref, set }} from "https://www.gstatic.com/firebasejs/11.1.0/firebase-database.js"; | |
// TODO: Add SDKs for Firebase products that you want to use | |
// https://firebase.google.com/docs/web/setup#available-libraries | |
console.log("Initializing Firebase"); | |
// Your web app's Firebase configuration | |
// For Firebase JS SDK v7.20.0 and later, measurementId is optional | |
const firebaseConfig = {{ | |
apiKey: "{os.getenv('FIREBASE_API_KEY')}", | |
authDomain: "{os.getenv('FIREBASE_AUTH_DOMAIN')}", | |
databaseURL: "{os.getenv('FIREBASE_DATABASE_URL')}", | |
projectId: "{os.getenv('FIREBASE_PROJECT_ID')}", | |
storageBucket: "{os.getenv('FIREBASE_STORAGE_BUCKET')}", | |
messagingSenderId: "{os.getenv('FIREBASE_MESSAGING_SENDER_ID')}", | |
appId: "{os.getenv('FIREBASE_APP_ID')}", | |
measurementId: "{os.getenv('FIREBASE_MEASUREMENT_ID')}" | |
}}; | |
// Initialize Firebase | |
const app = initializeApp(firebaseConfig); | |
const realtimeDB = getDatabase(app); | |
const rollAccount = "{os.getenv('ROLL_ACCOUNT')}"; | |
const COLLECTIONS = {{ | |
COLLAB_EDIT_LINK: "collab_link_handler", | |
}}; | |
// Event listener for click | |
document.addEventListener('click', function (event) {{ | |
var link = event.target.closest('a'); | |
event.preventDefault(); | |
if (link && link.href) {{ | |
// Parse the URL to extract 'st' and 'et' | |
const url = new URL(link.href); | |
const startTime = url.searchParams.get('st'); | |
const endTime = url.searchParams.get('et'); | |
const userId = url.searchParams.get('uid') || ""; | |
if (startTime || endTime) {{ | |
let components = url.pathname.split("/"); | |
let callId = components[2]; | |
let recordingSessionId = components[3]; | |
let data = {{ | |
startTime: parseInt(startTime, 10), | |
endTime: parseInt(endTime, 10), | |
}}; | |
console.log("Data to save:", data); | |
// Firebase reference | |
let reference = ref( | |
realtimeDB, | |
`${{rollAccount}}/${{COLLECTIONS.COLLAB_EDIT_LINK}}/${{userId}}/${{callId}}/${{recordingSessionId}}` | |
); | |
set(reference, data) | |
.then(() => {{ | |
console.log("Data saved successfully:", data); | |
}}) | |
.catch((error) => {{ | |
console.error("Error saving data:", error); | |
}}); | |
}} | |
}} | |
}}); | |
</script> | |
""" | |