Skip to content

Caso completo para testes em cenários e com base estatistica #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions gradioUserInterface/day2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,153 @@
"if __name__ == \"__main__\":\n",
" interface.launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eec21063",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import asyncio\n",
"import json\n",
"from typing import List, Dict\n",
"from time import time\n",
"from openai import OpenAI\n",
"from dotenv import load_dotenv\n",
"\n",
"# Carrega a variável de ambiente\n",
"load_dotenv()\n",
"openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"\n",
"# Lista de URLs para testar (cenários reais e estatísticos)\n",
"test_cases = [\n",
" {\n",
" \"name\": \"Empresa real com conteúdo completo\",\n",
" \"url\": \"https://stripe.com\",\n",
" \"expect_error\": False\n",
" },\n",
" {\n",
" \"name\": \"URL inválida\",\n",
" \"url\": \"htp://malformed-url\",\n",
" \"expect_error\": True\n",
" },\n",
" {\n",
" \"name\": \"Exemplo com pouco conteúdo\",\n",
" \"url\": \"https://example.com\",\n",
" \"expect_error\": False # Conteúdo mínimo, mas válido\n",
" },\n",
" {\n",
" \"name\": \"Página de login (sem conteúdo útil)\",\n",
" \"url\": \"https://accounts.google.com\",\n",
" \"expect_error\": True\n",
" },\n",
" {\n",
" \"name\": \"Site com muitos links\",\n",
" \"url\": \"https://www.ibm.com\",\n",
" \"expect_error\": False\n",
" },\n",
"]\n",
"\n",
"from openai import OpenAI\n",
"\n",
"class BrochureTester:\n",
" def __init__(self, api_key):\n",
" self.api_key = api_key\n",
" openai.api_key = self.api_key\n",
"\n",
" def create_brochure_stream(self, url: str, user_input: str = \"\"):\n",
" company_name = url.split(\"://\")[-1].split(\".\")[0].capitalize()\n",
"\n",
" messages = [\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": \"\"\"You are a professional brochure writer. Create engaging, well-structured brochures \n",
" with markdown formatting. Use proper markdown syntax for headings (##), bullet points (*), \n",
" emphasis (**bold**), and other formatting elements.\"\"\"\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": f\"\"\"Create a professional brochure for {company_name} (URL: {url}).\n",
"\n",
" Structure the brochure with these sections using proper markdown formatting:\n",
" \n",
" 1. Company Overview\n",
" 2. Key Products/Services\n",
" 3. Value Proposition\n",
" 4. Why Choose Us\n",
" 5. Contact Information\n",
"\n",
" Additional requirements: {user_input}\n",
"\n",
" Use markdown features:\n",
" - ## for section headings\n",
" - * for bullet points\n",
" - ** for bold text\n",
" - > for notable quotes or highlights\n",
" - --- for section separators\n",
" \"\"\"\n",
" }\n",
" ]\n",
"\n",
" try:\n",
" stream = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=messages,\n",
" max_tokens=800,\n",
" temperature=0.7,\n",
" top_p=1.0,\n",
" stream=True\n",
" )\n",
" full_output = \"\"\n",
" for chunk in stream:\n",
" if chunk.choices[0].delta.content is not None:\n",
" full_output += chunk.choices[0].delta.content\n",
" return full_output\n",
" except Exception as e:\n",
" return f\"### Error\\n\\n{str(e)}\"\n",
"\n",
"\n",
"def run_tests():\n",
" api_key = os.getenv(\"OPENAI_API_KEY\")\n",
" tester = BrochureTester(api_key)\n",
" results = []\n",
"\n",
" for test in test_cases:\n",
" print(f\"🔍 Testando: {test['name']}\")\n",
" start = time()\n",
" output = tester.create_brochure_stream(test[\"url\"])\n",
" duration = round(time() - start, 2)\n",
"\n",
" error_detected = \"### Error\" in output or \"failed\" in output.lower()\n",
"\n",
" passed = error_detected == test[\"expect_error\"]\n",
" status = \"Sucesso\" if passed else \"Falhou\"\n",
"\n",
" print(f\"{status} | Tempo: {duration}s\")\n",
" print(\"-\" * 50)\n",
" \n",
" results.append({\n",
" \"cenário\": test[\"name\"],\n",
" \"url\": test[\"url\"],\n",
" \"esperava_erro\": test[\"expect_error\"],\n",
" \"erro_detectado\": error_detected,\n",
" \"resultado\": status,\n",
" \"tempo_execucao\": duration\n",
" })\n",
"\n",
" return results\n",
"\n",
"\n",
"# Executa os testes\n",
"test_results = run_tests()\n",
"\n",
"# Exibe os resultados como tabela\n",
"import pandas as pd\n",
"df = pd.DataFrame(test_results)\n",
"df\n"
]
}
],
"metadata": {
Expand Down