chatbox / app.py
anaspro
upadte
441e4e3
raw
history blame
4.48 kB
# -*- coding: utf-8 -*-
import os
import torch
import transformers
from transformers import pipeline
import gradio as gr
import spaces
# Load system prompt from file
def load_system_prompt():
try:
with open('system_prompt.txt', 'r', encoding='utf-8') as f:
return f.read().strip()
except FileNotFoundError:
return "أنت مساعد ذكي مفيد."
DEFAULT_SYSTEM_PROMPT = load_system_prompt()
model_path = "unsloth/Meta-Llama-3.1-8B-Instruct-bnb-4bit"
# إذا كان فيه HF_TOKEN في البيئة
hf_token = os.getenv("HF_TOKEN")
# Initialize pipeline for chat
pipeline_model = pipeline(
"text-generation",
model=model_path,
device_map="auto",
token=hf_token,
trust_remote_code=True
)
def generate_with_pipeline(messages, max_new_tokens=256, temperature=0.7, top_p=0.9):
"""Generate response using the pipeline with messages format"""
# Apply chat template for unsloth models
prompt = pipeline_model.tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
outputs = pipeline_model(
prompt,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p,
do_sample=True,
return_full_text=False
)
return outputs[0]["generated_text"]
@spaces.GPU()
def generate_response(input_data, chat_history, max_new_tokens, temperature, top_p, top_k, repetition_penalty):
try:
# Build messages for the pipeline (without chat history)
messages = []
# Add system message
system_content = DEFAULT_SYSTEM_PROMPT
messages.append({"role": "system", "content": system_content})
# Add current user message only
messages.append({"role": "user", "content": input_data})
# Generate response using pipeline
response = generate_with_pipeline(
messages,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p
)
if not response:
response = "أهلاً! أنا أليكس مساعد خدمة العملاء. كيف أقدر أساعدك اليوم؟"
yield response
except Exception as e:
print(f"Error in generate_response: {e}")
import traceback
print(traceback.format_exc())
yield "أهلاً! أنا أليكس مساعد خدمة العملاء. كيف أقدر أساعدك اليوم؟"
demo = gr.ChatInterface(
fn=generate_response,
additional_inputs=[
gr.Slider(label="الحد الأقصى للكلمات الجديدة", minimum=64, maximum=4096, step=1, value=2048),
gr.Slider(label="درجة الحرارة", minimum=0.1, maximum=2.0, step=0.1, value=0.7),
gr.Slider(label="Top-p", minimum=0.05, maximum=1.0, step=0.05, value=0.9),
gr.Slider(label="Top-k", minimum=1, maximum=100, step=1, value=50),
gr.Slider(label="عقوبة التكرار", minimum=1.0, maximum=2.0, step=0.05, value=1.0)
],
examples=[
[{"text": "النت عندي معطل من الصبح، تقدر تساعدني؟"}],
[{"text": "عندي مشكلة بالاتصال بالواي فاي"}],
[{"text": "شنو الباقات المتوفرة عندكم؟"}],
[{"text": "كيف أعيد ضبط الجهاز؟"}],
[{"text": "My device is not working properly"}],
],
cache_examples=False,
type="messages",
title="دعم عملاء TechSolutions - مساعد أليكس (العراقي)",
description="""🤖 مساعد خدمة عملاء ذكي لـ TechSolutions
✨ المميزات:
- 🌐 دعم ثنائي اللغة (عربي وإنجليزي)
- 💬 لهجة محادثة طبيعية
- 🔧 دعم فني واستكشاف الأخطاء
- 📋 معلومات الخدمات والإرشاد
- 🎯 مدعوم بـ موديل Unsloth Meta-Llama-3.1-8B-Instruct-bnb-4bit (مع تحسينات الأداء والضغط)
احجي مع أليكس لحل مشاكلك التقنية، استفسر عن الخدمات، أو احصل على معلومات المنتجات.""",
fill_height=True,
textbox=gr.Textbox(
label="اكتب رسالتك هنا",
placeholder="مثال: عندي مشكلة بالجهاز..."
),
stop_btn="إيقاف التوليد",
multimodal=False,
theme=gr.themes.Soft()
)
if __name__ == "__main__":
demo.launch()