Upload dzgpt_app.py
Browse files- dzgpt_app.py +43 -0
dzgpt_app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
import torch
|
| 4 |
+
import datetime
|
| 5 |
+
|
| 6 |
+
# Chargement du modèle arabe DZ
|
| 7 |
+
model_name = "akhooli/gpt2-small-arabic"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 10 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 11 |
+
|
| 12 |
+
# Fonction principale avec historique
|
| 13 |
+
|
| 14 |
+
def chat_fn(message, history):
|
| 15 |
+
prompt = f"مستخدم: {message}\nالذكاء الاصطناعي:"
|
| 16 |
+
result = generator(prompt, max_length=100, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
|
| 17 |
+
response = result[0]["generated_text"].split("الذكاء الاصطناعي:")[-1].strip()
|
| 18 |
+
|
| 19 |
+
# Sauvegarde historique
|
| 20 |
+
with open("historique.txt", "a", encoding="utf-8") as f:
|
| 21 |
+
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 22 |
+
f.write(f"[{now}] Utilisateur: {message}\nRéponse: {response}\n---\n")
|
| 23 |
+
|
| 24 |
+
return response
|
| 25 |
+
|
| 26 |
+
# Interface Gradio avec logo
|
| 27 |
+
with gr.Blocks(theme="soft") as demo:
|
| 28 |
+
gr.Image(value="logo.png", show_label=False, show_download_button=False, container=False, height=150)
|
| 29 |
+
|
| 30 |
+
gr.ChatInterface(
|
| 31 |
+
fn=chat_fn,
|
| 32 |
+
title="DZGPT 🇩🇿 | دردش مع الذكاء الاصطناعي",
|
| 33 |
+
description="تحدث مع بوت ذكي باللهجة الجزائرية أو العربية. يدعم الصوت والردود المنطوقة.",
|
| 34 |
+
examples=["كيفاش داير؟", "أعطيني نكتة", "ترجملي للفرنسية"],
|
| 35 |
+
retry_btn="🔁 إعادة",
|
| 36 |
+
clear_btn="🧹 مسح",
|
| 37 |
+
submit_btn="🗣️ أرسل",
|
| 38 |
+
input_audio="microphone",
|
| 39 |
+
output_audio="auto",
|
| 40 |
+
textbox=gr.Textbox(placeholder="أكتب هنا...", container=True, scale=7)
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
demo.launch()
|