File size: 4,147 Bytes
f9385df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os
from textwrap import dedent
from typing import Dict, List, Optional

import gradio as gr
from dotenv import load_dotenv
from pydantic import BaseModel, Field
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.firecrawl import FirecrawlTools

load_dotenv()

# Variables d’environnement depuis Hugging Face "Secrets"
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
os.environ["FIRECRAWL_API_KEY"] = os.getenv("FIRECRAWL_API_KEY")

# Modèle de réponse structuré
class ContentSection(BaseModel):
    heading: Optional[str] = Field(None, description="Section heading")
    content: str = Field(..., description="Section content text")

class PageInformation(BaseModel):
    url: str
    title: str
    description: Optional[str]
    features: Optional[List[str]]
    content_sections: Optional[List[ContentSection]]
    links: Optional[Dict[str, str]]
    contact_info: Optional[Dict[str, str]]
    metadata: Optional[Dict[str, str]]
    feedback: Optional[str]

# Définition de l'agent
agent = Agent(
    model=OpenAIChat(id="gpt-4.1"),
    tools=[FirecrawlTools(scrape=True, crawl=False)],
    instructions=dedent("""
        Analysez la page fournie et extrayez les informations clés en respectant ce format :
        - Lisibilité
        - Ergonomie / navigation
        - Design
        - Sécurité
        - Pertinence des rubriques
        - Améliorations à suggérer
        Soyez clair et synthétique. Résumez les parties longues.
    """).strip(),
    response_model=PageInformation,
)

# Fonction utilisée par Gradio
def analyser_page(url: str):
    prompt = (
        f"Analyse la page {url} selon les critères suivants : lisibilité, ergonomie/navigation, "
        "design, sécurité, pertinence des rubriques, améliorations à suggérer. Résume et structure la réponse."
    )
    result = agent.run(prompt)
    print("DEBUG TYPE:", type(result))
    print("DEBUG VALUE:", result)

    # Utilise le vrai contenu structuré
    content = getattr(result, "content", None)
    if hasattr(content, "dict") and callable(content.dict):
        data = content.dict()
    elif isinstance(content, dict):
        data = content
    elif isinstance(content, str):
        return f"Réponse brute :\n\n{content}"
    else:
        return f"Type inattendu pour content : {type(content)}\nValeur : {content}"

    # Construction d'un markdown structuré
    markdown = f"# Analyse de la page\n\n"
    markdown += f"**URL :** {data.get('url', url)}\n\n"
    if data.get("title"):
        markdown += f"**Titre :** {data['title']}\n\n"
    if data.get("description"):
        markdown += f"**Description :** {data['description']}\n\n"
    if data.get("features"):
        markdown += "## Fonctionnalités\n"
        for feat in data["features"]:
            markdown += f"- {feat}\n"
        markdown += "\n"
    if data.get("content_sections"):
        for section in data["content_sections"]:
            if section.get("heading"):
                markdown += f"## {section['heading']}\n"
            markdown += f"{section['content']}\n\n"
    if data.get("links"):
        markdown += "## Liens\n"
        for name, link in data["links"].items():
            markdown += f"- [{name}]({link})\n"
        markdown += "\n"
    if data.get("contact_info"):
        markdown += "## Contact\n"
        for k, v in data["contact_info"].items():
            markdown += f"- **{k}** : {v}\n"
        markdown += "\n"
    if data.get("metadata"):
        markdown += "## Métadonnées\n"
        for k, v in data["metadata"].items():
            markdown += f"- **{k}** : {v}\n"
        markdown += "\n"
    if data.get("feedback"):
        markdown += f"## Feedback\n{data['feedback']}\n"

    return markdown

# Interface Gradio
demo = gr.Interface(
    fn=analyser_page,
    inputs=gr.Textbox(label="URL à analyser", placeholder="https://www.exemple.com"),
    outputs=gr.Markdown(),  # Affichage markdown structuré
    title="🧠 Analyseur de Page Web",
    description="Entrez une URL à analyser selon des critères UX/UI.",
)

if __name__ == "__main__":
    demo.launch()