File size: 4,248 Bytes
ac450cf
 
 
e0dbd75
ac450cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c2e5c2
ac450cf
 
 
1c2e5c2
ac450cf
 
1c2e5c2
 
ac450cf
 
 
 
 
 
 
 
1c2e5c2
 
 
 
ac450cf
1c2e5c2
ac450cf
 
 
 
 
1c2e5c2
ac450cf
 
1c2e5c2
ac450cf
 
 
 
 
 
1c2e5c2
 
e0dbd75
1c2e5c2
 
 
 
ac450cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c2e5c2
ac450cf
 
1c2e5c2
ac450cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c2e5c2
ac450cf
 
 
 
1c2e5c2
 
ac450cf
 
 
 
1c2e5c2
ac450cf
 
 
 
 
1c2e5c2
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import gradio as gr
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from deep_translator import GoogleTranslator
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
from ddgs import DDGS

# =========================
# ✅ MODEL
# =========================
model_id = "Qwen/Qwen2.5-0.5B-Instruct"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    device_map="auto",
    dtype=torch.float16
)

# =========================
# ✅ SEARCH (4 texte + 1 image)
# =========================
def search_wiki(query):
    text_results = []
    image_url = None

    with DDGS() as ddgs:
        # ✅ 4 résultats texte
        results = list(ddgs.text(query, max_results=2))

        for r in results:
            text_results.append({
                "title": r.get("title"),
                "link": r.get("href"),
                "description": r.get("body")
            })

        # ✅ 1 image (5e résultat)
        images = list(ddgs.images(query, max_results=2))
        if images:
            image_url = images[0].get("image")

    return text_results, image_url

# =========================
# ✅ PIPELINE
# =========================
def run_pipeline(user_query):
    results, img = search_wiki(user_query)

    if not results:
        return "❌ Aucun résultat trouvé.", None

    link = results[0]["link"]

    try:
        options = Options()
        options.add_argument("--headless")
        options.add_argument("--no-sandbox") # Obligatoire pour Docker
        options.add_argument("--disable-dev-shm-usage") # Obligatoire pour Docker

        # Sur HF Spaces, le driver est installé dans /usr/bin/chromedriver
        service = Service("/usr/bin/chromedriver") 

        driver = webdriver.Chrome(service=service,options=options)
        driver.get(link)

        paragraphs = driver.find_elements(By.TAG_NAME, "p")

        translator = GoogleTranslator(source='auto', target='fr')
        texte_total = ""

        for p in paragraphs:
            texte = p.text.strip()
            if texte and len(texte) > 50:
                try:
                    traduction = translator.translate(texte)
                    texte_total += traduction + "\n"
                except:
                    pass

        driver.quit()

        texte_total = texte_total[:6000]

        prompt = (
            "Fais un résumé clair et structuré en français :\n\n"
            + texte_total
        )

        inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

        outputs = model.generate(
            **inputs,
            max_new_tokens=300,
            temperature=0.7,
            do_sample=True
        )

        response = tokenizer.decode(outputs[0], skip_special_tokens=True)

        return f"🔗 {link}\n\n📄 {response}", img

    except Exception as e:
        return f"❌ Erreur : {str(e)}", None

# =========================
# ✅ STYLE
# =========================
css = """
body { background: #0f1117; color: white; }

.container {
    max-width: 900px;
    margin: auto;
    padding-top: 40px;
}

.title {
    text-align: center;
    font-size: 30px;
    font-weight: bold;
    margin-bottom: 20px;
}

textarea {
    background: #1a1d26 !important;
    color: white !important;
    border-radius: 12px !important;
}

button {
    background: linear-gradient(90deg, #00c6ff, #0072ff) !important;
    border-radius: 12px !important;
}
"""

# =========================
# ✅ UI
# =========================
with gr.Blocks(css=css) as app:

    with gr.Column(elem_classes="container"):
        gr.Markdown("<div class='title'>🚀 KTXStudio AI</div>")

        query = gr.Textbox(
            placeholder="Ex : Ninjago Dragon Rising saison 4"
        )

        btn = gr.Button("⚡ Générer")

        output_text = gr.Textbox(lines=15)
        output_img = gr.Image(label="Image (résultat 5)")

        btn.click(
            run_pipeline,
            inputs=query,
            outputs=[output_text, output_img]
        )

# =========================
# ✅ RUN
# =========================
app.launch(share=True,favicon_path="favicon.png")