Update app.py
Browse files
app.py
CHANGED
|
@@ -1,132 +1,85 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import time
|
| 4 |
-
import requests
|
| 5 |
import gradio as gr
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
CU_VERSION = "2025-05-01-preview"
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
def safe_json(resp: requests.Response):
|
| 12 |
-
try:
|
| 13 |
-
return resp.json()
|
| 14 |
-
except Exception:
|
| 15 |
-
return {"text": resp.text}
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
key = os.getenv("KEY")
|
| 27 |
-
analyzer = os.getenv("ANALYZER_NAME")
|
| 28 |
-
missing = [n for n, v in [("ENDPOINT", endpoint), ("KEY", key), ("ANALYZER_NAME", analyzer)] if not v]
|
| 29 |
-
return endpoint, key, analyzer, missing
|
| 30 |
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
put_resp = requests.put(url, headers=headers, data=schema, timeout=60)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
return (
|
| 45 |
-
False,
|
| 46 |
-
f"Missing Operation-Location header.\n"
|
| 47 |
-
f"delete status: {del_resp.status_code}\n"
|
| 48 |
-
f"create status: {put_resp.status_code}\n"
|
| 49 |
-
f"create body: {json.dumps(safe_json(put_resp), indent=2)}"
|
| 50 |
-
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
return (
|
| 61 |
-
True,
|
| 62 |
-
f"Analyzer '{analyzer}' created successfully.\n"
|
| 63 |
-
f"delete status: {del_resp.status_code}\n"
|
| 64 |
-
f"create status: {put_resp.status_code}\n"
|
| 65 |
-
f"final status: {status}"
|
| 66 |
-
)
|
| 67 |
-
return (
|
| 68 |
-
False,
|
| 69 |
-
f"Analyzer creation failed.\n"
|
| 70 |
-
f"delete status: {del_resp.status_code}\n"
|
| 71 |
-
f"create status: {put_resp.status_code}\n"
|
| 72 |
-
f"final status: {status}\n"
|
| 73 |
-
f"details:\n{json.dumps(j, indent=2)}"
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
return (
|
| 77 |
-
False,
|
| 78 |
-
f"Timed out waiting for analyzer creation.\n"
|
| 79 |
-
f"delete status: {del_resp.status_code}\n"
|
| 80 |
-
f"create status: {put_resp.status_code}"
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
def handle_message(user_text, chat_history):
|
| 85 |
-
"""
|
| 86 |
-
Gradio ChatInterface handler signature:
|
| 87 |
-
(message: str, history: list[tuple[str,str]]) -> str | tuple
|
| 88 |
-
We'll append to history outside via ChatInterface; just return assistant text.
|
| 89 |
-
"""
|
| 90 |
-
text = (user_text or "").strip().lower()
|
| 91 |
-
|
| 92 |
-
if text in {"help", "/help", "commands"}:
|
| 93 |
-
return (
|
| 94 |
-
"Commands:\n"
|
| 95 |
-
"- **create analyzer** : delete (if exists) + create analyzer from `biz-card.json`\n"
|
| 96 |
-
"- **show config** : show whether ENDPOINT/KEY/ANALYZER_NAME are set\n"
|
| 97 |
-
"- **help** : show this message"
|
| 98 |
)
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
endpoint, key, analyzer, missing = load_config()
|
| 102 |
-
if missing:
|
| 103 |
-
return f"Missing env vars: {', '.join(missing)}"
|
| 104 |
-
return f"Config loaded ✅\n- ENDPOINT: {endpoint}\n- ANALYZER_NAME: {analyzer}\n- KEY: (set)"
|
| 105 |
-
|
| 106 |
-
if text in {"create analyzer", "create", "make analyzer", "build analyzer"}:
|
| 107 |
-
endpoint, key, analyzer, missing = load_config()
|
| 108 |
-
if missing:
|
| 109 |
-
return f"Cannot create analyzer. Missing env vars: {', '.join(missing)}"
|
| 110 |
-
|
| 111 |
try:
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
return
|
| 118 |
-
|
| 119 |
-
return "Type **help** to see commands. Try **create analyzer**."
|
| 120 |
|
| 121 |
|
| 122 |
demo = gr.ChatInterface(
|
| 123 |
fn=handle_message,
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
if __name__ == "__main__":
|
| 132 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
|
|
|
|
| 5 |
|
| 6 |
+
def _normalize_endpoint(endpoint: str) -> str:
|
| 7 |
+
endpoint = (endpoint or "").strip()
|
| 8 |
+
return endpoint[:-1] if endpoint.endswith("/") else endpoint
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def _azure_chat_completions(endpoint, api_key, model, messages, temperature=0.7, max_tokens=600, timeout=60):
|
| 12 |
+
endpoint = _normalize_endpoint(endpoint)
|
| 13 |
+
url = f"{endpoint}/openai/v1/chat/completions"
|
| 14 |
|
| 15 |
+
headers = {
|
| 16 |
+
"Content-Type": "application/json",
|
| 17 |
+
"api-key": api_key.strip(),
|
| 18 |
+
}
|
| 19 |
|
| 20 |
+
payload = {
|
| 21 |
+
"model": model.strip(),
|
| 22 |
+
"messages": messages,
|
| 23 |
+
"temperature": float(temperature),
|
| 24 |
+
"max_tokens": int(max_tokens),
|
| 25 |
+
}
|
| 26 |
|
| 27 |
+
r = requests.post(url, headers=headers, json=payload, timeout=timeout)
|
| 28 |
+
r.raise_for_status()
|
| 29 |
+
return r.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
+
def handle_message(message, history, endpoint, api_key, model, system_prompt, temperature, max_tokens):
|
| 33 |
+
# history is list of [user, assistant] pairs
|
| 34 |
+
messages = []
|
| 35 |
+
if system_prompt and system_prompt.strip():
|
| 36 |
+
messages.append({"role": "system", "content": system_prompt.strip()})
|
| 37 |
|
| 38 |
+
for u, a in history:
|
| 39 |
+
if u:
|
| 40 |
+
messages.append({"role": "user", "content": u})
|
| 41 |
+
if a:
|
| 42 |
+
messages.append({"role": "assistant", "content": a})
|
| 43 |
|
| 44 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
| 45 |
|
| 46 |
+
if not endpoint or not api_key or not model:
|
| 47 |
+
return "Please provide Endpoint, API Key, and Model."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
try:
|
| 50 |
+
data = _azure_chat_completions(
|
| 51 |
+
endpoint=endpoint,
|
| 52 |
+
api_key=api_key,
|
| 53 |
+
model=model,
|
| 54 |
+
messages=messages,
|
| 55 |
+
temperature=temperature,
|
| 56 |
+
max_tokens=max_tokens,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
+
return data["choices"][0]["message"]["content"]
|
| 59 |
|
| 60 |
+
except requests.HTTPError as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
try:
|
| 62 |
+
err_json = e.response.json()
|
| 63 |
+
return f"HTTP {e.response.status_code}: {json.dumps(err_json, indent=2)}"
|
| 64 |
+
except Exception:
|
| 65 |
+
return f"HTTP error: {str(e)}"
|
| 66 |
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
demo = gr.ChatInterface(
|
| 72 |
fn=handle_message,
|
| 73 |
+
additional_inputs=[
|
| 74 |
+
gr.Textbox(label="Azure OpenAI Endpoint", placeholder="https://ai-hubXXXX.openai.azure.com"),
|
| 75 |
+
gr.Textbox(label="API Key", placeholder="paste your key here", type="password"),
|
| 76 |
+
gr.Textbox(label="Model (or deployment name)", placeholder="e.g., gpt-4o-mini"),
|
| 77 |
+
gr.Textbox(label="System prompt (optional)", value="You are a helpful assistant."),
|
| 78 |
+
gr.Slider(0.0, 1.5, value=0.7, step=0.1, label="Temperature"),
|
| 79 |
+
gr.Slider(50, 2000, value=600, step=50, label="Max tokens"),
|
| 80 |
+
],
|
| 81 |
+
title="Azure OpenAI Chat Completion",
|
| 82 |
+
description="Enter your Azure OpenAI endpoint + key, then chat.",
|
| 83 |
)
|
| 84 |
|
| 85 |
+
demo.launch()
|
|
|
|
|
|