Spaces:
Sleeping
Sleeping
samir72
commited on
Commit
·
586cbc6
1
Parent(s):
c96bf89
Prompts read from json file
Browse files- app.py +44 -4
- metadata.json +23 -0
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from datetime import datetime
|
|
| 6 |
import gradio as gr
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from openai import AzureOpenAI # official OpenAI SDK, works with Azure endpoints
|
|
|
|
| 9 |
|
| 10 |
# --- LLM call (Azure OpenAI with API key) -----------------------------------
|
| 11 |
|
|
@@ -22,6 +23,7 @@ def summarize_audio_b64(audio_b64: str, sys_prompt: str, user_prompt: str) -> st
|
|
| 22 |
|
| 23 |
if not endpoint or not api_key or not deployment:
|
| 24 |
return "Server misconfiguration: required env vars missing."
|
|
|
|
| 25 |
|
| 26 |
try:
|
| 27 |
client = AzureOpenAI(
|
|
@@ -56,8 +58,30 @@ def summarize_audio_b64(audio_b64: str, sys_prompt: str, user_prompt: str) -> st
|
|
| 56 |
|
| 57 |
except Exception as ex:
|
| 58 |
return print(f"Error from Azure OpenAI: {ex}")
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# --- I/O helpers ------------------------------------------------------------
|
| 62 |
|
| 63 |
def encode_audio_from_path(path: str) -> str:
|
|
@@ -120,15 +144,31 @@ with gr.Blocks(title="Audio Summarizer") as demo:
|
|
| 120 |
with gr.Column():
|
| 121 |
url_input = gr.Textbox(label="mp3 URL", placeholder="https://example.com/audio.mp3")
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
with gr.Row():
|
| 124 |
userprompt_input = gr.Textbox(
|
| 125 |
label="User Prompt",
|
| 126 |
-
value="Summarize the audio content",
|
|
|
|
| 127 |
placeholder="e.g., Extract key points and action items",
|
| 128 |
)
|
| 129 |
sysprompt_input = gr.Textbox(
|
| 130 |
label="System Prompt",
|
| 131 |
-
value="You are an AI assistant with a
|
|
|
|
| 132 |
)
|
| 133 |
|
| 134 |
submit_btn = gr.Button("Summarize")
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from openai import AzureOpenAI # official OpenAI SDK, works with Azure endpoints
|
| 9 |
+
import json
|
| 10 |
|
| 11 |
# --- LLM call (Azure OpenAI with API key) -----------------------------------
|
| 12 |
|
|
|
|
| 23 |
|
| 24 |
if not endpoint or not api_key or not deployment:
|
| 25 |
return "Server misconfiguration: required env vars missing."
|
| 26 |
+
|
| 27 |
|
| 28 |
try:
|
| 29 |
client = AzureOpenAI(
|
|
|
|
| 58 |
|
| 59 |
except Exception as ex:
|
| 60 |
return print(f"Error from Azure OpenAI: {ex}")
|
| 61 |
+
#pass
|
| 62 |
+
|
| 63 |
+
#----Retrieve meta data from metadata.json file------------------------------
|
| 64 |
+
def retrieve_file_path(file_name):
|
| 65 |
+
path = os.path.dirname(os.path.abspath(__file__))
|
| 66 |
+
file_path = os.path.join(path, file_name)
|
| 67 |
+
if os.path.isfile(file_path):
|
| 68 |
+
return file_path
|
| 69 |
+
elif not os.path.exists(file_path):
|
| 70 |
+
print(f"'{file_path}' does not exist.")
|
| 71 |
+
return None
|
| 72 |
+
return None
|
| 73 |
+
|
| 74 |
+
def retrieve_json_record(file_path, record_id):
|
| 75 |
+
with open(file_path, 'r') as file:
|
| 76 |
+
data = json.load(file)
|
| 77 |
+
if isinstance(data, list):
|
| 78 |
+
for record in data:
|
| 79 |
+
if record.get('metadata', {}).get('id') == record_id:
|
| 80 |
+
return record
|
| 81 |
+
elif isinstance(data, dict):
|
| 82 |
+
if data.get('metadata', {}).get('id') == record_id:
|
| 83 |
+
return data
|
| 84 |
+
return None
|
| 85 |
# --- I/O helpers ------------------------------------------------------------
|
| 86 |
|
| 87 |
def encode_audio_from_path(path: str) -> str:
|
|
|
|
| 144 |
with gr.Column():
|
| 145 |
url_input = gr.Textbox(label="mp3 URL", placeholder="https://example.com/audio.mp3")
|
| 146 |
|
| 147 |
+
### Get system and user prompts from metadata.json file
|
| 148 |
+
file_name = 'metadata.json'
|
| 149 |
+
record_id = '1'
|
| 150 |
+
file_path = retrieve_file_path(file_name)
|
| 151 |
+
|
| 152 |
+
jsonrecord = retrieve_json_record(file_path, record_id)
|
| 153 |
+
if jsonrecord:
|
| 154 |
+
print(json.dumps(jsonrecord, indent=2))
|
| 155 |
+
else:
|
| 156 |
+
print("Record not found.")
|
| 157 |
+
|
| 158 |
+
sysprompt_default = jsonrecord['metadata']['content']['system_prompt']['content']
|
| 159 |
+
userprompt_default = jsonrecord['metadata']['content']['user_prompt']['content']
|
| 160 |
+
|
| 161 |
with gr.Row():
|
| 162 |
userprompt_input = gr.Textbox(
|
| 163 |
label="User Prompt",
|
| 164 |
+
#value="Summarize the audio content",
|
| 165 |
+
value=userprompt_default,
|
| 166 |
placeholder="e.g., Extract key points and action items",
|
| 167 |
)
|
| 168 |
sysprompt_input = gr.Textbox(
|
| 169 |
label="System Prompt",
|
| 170 |
+
#value="You are an AI assistant with a charter to clearly analyze the customer enquiry.",
|
| 171 |
+
value=sysprompt_default,
|
| 172 |
)
|
| 173 |
|
| 174 |
submit_btn = gr.Button("Summarize")
|
metadata.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"metadata": {
|
| 3 |
+
"id": "1",
|
| 4 |
+
"timestamp": "2025-09-18T11:24:00Z",
|
| 5 |
+
"type": "prompt_metadata",
|
| 6 |
+
"content": {
|
| 7 |
+
"system_prompt": {
|
| 8 |
+
"title": "System Prompt",
|
| 9 |
+
"content": "You are a highly capable AI assistant designed to process and analyze multimodal inputs, including text and images. Your task is to generate a structured response based on the provided input, following a specific format. When an image is uploaded along with a text prompt, analyze the image content and integrate it with the text to produce a coherent response. Use the following format for your response: - **Summary**: Provide a concise summary of the main points from the text and image (if applicable), limited to 2-3 sentences. - **Key Details**: List 3-5 key details extracted from the text and image, presented as bullet points. - **Insights**: Offer 1-2 insightful observations or conclusions based on the content, keeping it brief. Ensure your response is accurate, relevant, and tailored to the input. If the input lacks sufficient information, acknowledge the limitation and provide a general response based on the available data.",
|
| 10 |
+
"version": "1.0",
|
| 11 |
+
"created_at": "2025-09-18T11:00:00Z"
|
| 12 |
+
},
|
| 13 |
+
"user_prompt": {
|
| 14 |
+
"title": "User Prompt",
|
| 15 |
+
"content": "Analyze the attached image and the following text to provide a structured response. Make sure to include a summary, key details, and insights based on the combined information.",
|
| 16 |
+
"version": "1.0",
|
| 17 |
+
"created_at": "2025-09-18T11:10:00Z"
|
| 18 |
+
}
|
| 19 |
+
},
|
| 20 |
+
"tags": ["AI", "multimodal", "podcast", "AWS"],
|
| 21 |
+
"status": "active"
|
| 22 |
+
}
|
| 23 |
+
}
|