Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# --- Config (HF Spaces: set Repository Secret GROQ_API_KEY) ---
|
| 6 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 7 |
+
if not groq_api_key:
|
| 8 |
+
raise RuntimeError(
|
| 9 |
+
"Missing GROQ_API_KEY. In your Space, go to Settings → Repository secrets → Add 'GROQ_API_KEY'."
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 13 |
+
headers = {
|
| 14 |
+
"Authorization": f"Bearer {groq_api_key}",
|
| 15 |
+
"Content-Type": "application/json",
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
LOGO_URL = "https://raw.githubusercontent.com/Decoding-Data-Science/Omantel/main/download%20(36).png"
|
| 19 |
+
|
| 20 |
+
OMANTEL_SYSTEM = """You are Aisha, the Omantel AI assistant for developer enablement.
|
| 21 |
+
|
| 22 |
+
Style
|
| 23 |
+
- Be concise, professional, and action-oriented.
|
| 24 |
+
- Structure all answers as: Summary → Steps → Code → Notes.
|
| 25 |
+
- Prefer bullet points over long paragraphs.
|
| 26 |
+
|
| 27 |
+
Domain
|
| 28 |
+
- Focus on telecom/customer-care use cases: RAG over KB articles, intent classification, call/chat summarization, ticket triage, PII redaction, and evaluation.
|
| 29 |
+
- Show Python examples (requests, FastAPI, Gradio) and Groq (Llama 3.1-8B).
|
| 30 |
+
- Use realistic ops/QA metrics when relevant: FRT, AHT, intent accuracy, containment/deflection rate, CSAT, latency (p95), and token usage.
|
| 31 |
+
|
| 32 |
+
Safety & Privacy
|
| 33 |
+
- Do not invent facts; say “I’m not sure” if uncertain.
|
| 34 |
+
- Never include real keys, internal URLs, or customer data. Use placeholders.
|
| 35 |
+
- Redact PII patterns in examples (MSISDN, IMSI, national IDs, emails).
|
| 36 |
+
- Assume all data is confidential; follow least-privilege principles.
|
| 37 |
+
|
| 38 |
+
Localization
|
| 39 |
+
- Default to English. If the user writes in Arabic, reply in Arabic.
|
| 40 |
+
- Use Asia/Muscat timezone for dates/times when needed.
|
| 41 |
+
|
| 42 |
+
Formatting
|
| 43 |
+
- Put code in fenced blocks with minimal, runnable examples.
|
| 44 |
+
- Include curl when helpful.
|
| 45 |
+
- Add clear TODOs for env vars and secrets.
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
def call_groq(messages, temperature=0.2, max_tokens=600, model="llama-3.1-8b-instant"):
|
| 49 |
+
body = {
|
| 50 |
+
"model": model,
|
| 51 |
+
"messages": messages,
|
| 52 |
+
"temperature": temperature,
|
| 53 |
+
"max_tokens": max_tokens,
|
| 54 |
+
}
|
| 55 |
+
try:
|
| 56 |
+
resp = requests.post(url, headers=headers, json=body, timeout=60)
|
| 57 |
+
except requests.RequestException as e:
|
| 58 |
+
return f"Error: network/request failed: {e}"
|
| 59 |
+
if resp.status_code == 200:
|
| 60 |
+
return resp.json()["choices"][0]["message"]["content"]
|
| 61 |
+
else:
|
| 62 |
+
try:
|
| 63 |
+
return f"Error: {resp.status_code} {resp.json()}"
|
| 64 |
+
except Exception:
|
| 65 |
+
return f"Error: {resp.status_code} {resp.text}"
|
| 66 |
+
|
| 67 |
+
def groq_chat(message, history):
|
| 68 |
+
messages = [{"role": "system", "content": OMANTEL_SYSTEM}]
|
| 69 |
+
for u, a in history:
|
| 70 |
+
if u:
|
| 71 |
+
messages.append({"role": "user", "content": u})
|
| 72 |
+
if a:
|
| 73 |
+
messages.append({"role": "assistant", "content": a})
|
| 74 |
+
messages.append({"role": "user", "content": message})
|
| 75 |
+
return call_groq(messages)
|
| 76 |
+
|
| 77 |
+
# --- Minimal UI with Submit button (structure preserved) ---
|
| 78 |
+
with gr.Blocks(title="Omantel Developer Assistant") as app:
|
| 79 |
+
gr.HTML("""
|
| 80 |
+
<style>
|
| 81 |
+
.header-row { display:flex; align-items:center; gap:12px; }
|
| 82 |
+
.header-logo img { max-height:48px; border-radius:8px; }
|
| 83 |
+
.header-title { font-size:20px; font-weight:600; margin:0; }
|
| 84 |
+
</style>
|
| 85 |
+
""")
|
| 86 |
+
|
| 87 |
+
with gr.Row(elem_classes=["header-row"]):
|
| 88 |
+
gr.Image(
|
| 89 |
+
value=LOGO_URL, label=None, show_label=False,
|
| 90 |
+
interactive=False, height=100, elem_classes=["header-logo"]
|
| 91 |
+
)
|
| 92 |
+
title_box = gr.Textbox(value="Omantel Developer Assistant (Aisha)", label="Title", lines=1)
|
| 93 |
+
header_md = gr.Markdown("### Omantel Developer Assistant (Aisha)", elem_classes=["header-title"])
|
| 94 |
+
|
| 95 |
+
def _update_header(t):
|
| 96 |
+
return f"### {t}" if t.strip() else "### Omantel Developer Assistant (Aisha)"
|
| 97 |
+
title_box.change(_update_header, inputs=title_box, outputs=header_md)
|
| 98 |
+
|
| 99 |
+
gr.Markdown("Ask about telecom/customer-care AI use cases. Answers follow **Summary → Steps → Code → Notes**.")
|
| 100 |
+
|
| 101 |
+
chatbot = gr.Chatbot(height=420)
|
| 102 |
+
user_in = gr.Textbox(placeholder="Type your question…", lines=2, label="Your message")
|
| 103 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 104 |
+
history_state = gr.State([]) # list of (user, assistant)
|
| 105 |
+
|
| 106 |
+
def on_submit(user_msg, history):
|
| 107 |
+
history = history or []
|
| 108 |
+
if not user_msg or not user_msg.strip():
|
| 109 |
+
return history, gr.update(), ""
|
| 110 |
+
bot_reply = groq_chat(user_msg.strip(), history)
|
| 111 |
+
history.append((user_msg.strip(), bot_reply))
|
| 112 |
+
return history, gr.update(value=history), ""
|
| 113 |
+
|
| 114 |
+
submit_btn.click(
|
| 115 |
+
fn=on_submit,
|
| 116 |
+
inputs=[user_in, history_state],
|
| 117 |
+
outputs=[history_state, chatbot, user_in],
|
| 118 |
+
api_name=False
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# HF Spaces: bind to 0.0.0.0
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
app.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))
|