{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 🧪 Exhibition Connector RAG — Live API & Hugging Face Test\n",
        "\n",
        "این نوت‌بوک سبک برای تست نسخه‌ی جدید پروژه است:\n",
        "\n",
        "- Vercel API backend\n",
        "- Hugging Face static frontend\n",
        "- RAG/Search endpoint\n",
        "- Scraper endpoint\n",
        "- خروجی‌های نمونه برای شرکت‌ها\n",
        "\n",
        "بدون نیاز به GPU، FAISS یا توکن Hugging Face اجرا می‌شود.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# تنظیم لینک‌ها\n",
        "VERCEL_API_BASE = \"https://vercel-app-amber-five.vercel.app\"\n",
        "HF_FRONTEND_URL = \"https://sosa123456-exhibition-connector-rag1-static.static.hf.space/index.html?v=20260726-10\"\n",
        "\n",
        "print(\"Vercel API:\", VERCEL_API_BASE)\n",
        "print(\"Hugging Face Frontend:\", HF_FRONTEND_URL)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import requests, json, textwrap, pandas as pd\n",
        "\n",
        "def get_json(url, **params):\n",
        "    r = requests.get(url, params=params, timeout=45)\n",
        "    print(\"GET\", r.url)\n",
        "    print(\"status:\", r.status_code)\n",
        "    r.raise_for_status()\n",
        "    return r.json()\n",
        "\n",
        "def post_json(url, payload):\n",
        "    r = requests.post(url, json=payload, timeout=45)\n",
        "    print(\"POST\", url)\n",
        "    print(\"status:\", r.status_code)\n",
        "    r.raise_for_status()\n",
        "    return r.json()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1) تست سلامت API"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "health = get_json(f\"{VERCEL_API_BASE}/api/health\")\n",
        "health\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2) تست RAG/Search با چند سؤال"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "queries = [\n",
        "    \"وب‌سایت شرکت پریسماتک چیست؟\",\n",
        "    \"مواد شیمیایی تصفیه آب\",\n",
        "    \"شرکت‌های مرتبط با ابزار دقیق کدامند؟\",\n",
        "    \"سالن 31B\",\n",
        "]\n",
        "\n",
        "rows = []\n",
        "for q in queries:\n",
        "    data = get_json(f\"{VERCEL_API_BASE}/api/search\", q=q, limit=5)\n",
        "    for rank, item in enumerate(data.get(\"results\", [])[:5], start=1):\n",
        "        rows.append({\n",
        "            \"query\": q,\n",
        "            \"rank\": rank,\n",
        "            \"score\": round(float(item.get(\"score\", 0)), 2),\n",
        "            \"company\": item.get(\"company\"),\n",
        "            \"hall\": item.get(\"hall\") or \"—\",\n",
        "            \"booth\": item.get(\"booth\") or \"—\",\n",
        "            \"website\": item.get(\"website\") or \"—\",\n",
        "        })\n",
        "\n",
        "df = pd.DataFrame(rows)\n",
        "df\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3) تست دقیق پریسماتک باید رتبه اول باشد"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "prisma = get_json(f\"{VERCEL_API_BASE}/api/search\", q=\"وب‌سایت شرکت پریسماتک چیست؟\", limit=3)\n",
        "first = prisma[\"results\"][0]\n",
        "print(\"Top result:\", first[\"company\"], first.get(\"website\"))\n",
        "assert \"پریسماتک\" in first[\"company\"], \"❌ پریسماتک رتبه اول نیست\"\n",
        "print(\"✅ تست پریسماتک موفق بود\")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4) تست Scraper شرکت / URL"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "scrape = get_json(f\"{VERCEL_API_BASE}/api/scrape\", url=\"https://prismatech.ir/\")\n",
        "print(\"title:\", scrape[\"scraped\"].get(\"title\"))\n",
        "print(\"emails:\", scrape[\"scraped\"].get(\"emails\"))\n",
        "print(\"phones:\", scrape[\"scraped\"].get(\"phones\")[:3])\n",
        "print(\"snippet:\", scrape[\"scraped\"].get(\"textSnippet\", \"\")[:300])\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5) تست search + scrape همزمان"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "combo = get_json(f\"{VERCEL_API_BASE}/api/search\", q=\"پریسماتک\", limit=3, scrape=1)\n",
        "print(\"results:\", [r[\"company\"] for r in combo.get(\"results\", [])])\n",
        "print(\"scraped title:\", combo.get(\"scrapedUpdate\", {}).get(\"title\"))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6) تست فرانت Hugging Face"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "r = requests.get(HF_FRONTEND_URL, timeout=45)\n",
        "print(\"status:\", r.status_code)\n",
        "print(\"bytes:\", len(r.text))\n",
        "assert r.status_code == 200\n",
        "assert \"Exhibition Connector\" in r.text or \"پرتال\" in r.text\n",
        "print(\"✅ فرانت Hugging Face در دسترس است\")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 7) خلاصه نتیجه تست"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "summary = {\n",
        "    \"api_ok\": health.get(\"ok\"),\n",
        "    \"companies\": health.get(\"stats\", {}).get(\"companies\"),\n",
        "    \"halls\": health.get(\"stats\", {}).get(\"halls\"),\n",
        "    \"frontend_url\": HF_FRONTEND_URL,\n",
        "    \"api_url\": VERCEL_API_BASE,\n",
        "}\n",
        "summary\n"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}