anaspro
commited on
Commit
·
11b979a
1
Parent(s):
a535d94
updatE
Browse files
app.py
CHANGED
|
@@ -1,34 +1,152 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import os
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
model_path = "EzioDevio/iraqi_dialect_llm" # Use the Hugging Face Hub model path
|
| 7 |
|
| 8 |
-
#
|
| 9 |
hf_token = os.getenv("HF_TOKEN")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
generated = text_generator(input_text, max_length=50, num_return_sequences=1)
|
| 19 |
-
return generated[0]["generated_text"]
|
| 20 |
-
|
| 21 |
-
# Set up the Gradio interface
|
| 22 |
-
iface = gr.Interface(
|
| 23 |
-
fn=generate_text,
|
| 24 |
-
inputs="text",
|
| 25 |
-
outputs="text",
|
| 26 |
-
title="Iraqi Dialect Language Model",
|
| 27 |
-
description="Generate text in Iraqi Arabic dialect.",
|
| 28 |
-
examples=["شلونك اليوم؟", "وين رايح؟", "صباح الخير"]
|
| 29 |
)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
+
import torch
|
| 5 |
+
import transformers
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import spaces
|
| 9 |
+
|
| 10 |
+
# Load system prompt from file
|
| 11 |
+
def load_system_prompt():
|
| 12 |
+
try:
|
| 13 |
+
with open('system_prompt.txt', 'r', encoding='utf-8') as f:
|
| 14 |
+
return f.read().strip()
|
| 15 |
+
except FileNotFoundError:
|
| 16 |
+
return "أنت مساعد ذكي مفيد."
|
| 17 |
+
|
| 18 |
+
DEFAULT_SYSTEM_PROMPT = load_system_prompt()
|
| 19 |
|
| 20 |
+
model_path = "unsloth/Meta-Llama-3.1-8B-Instruct-bnb-4bit"
|
|
|
|
| 21 |
|
| 22 |
+
# إذا كان فيه HF_TOKEN في البيئة
|
| 23 |
hf_token = os.getenv("HF_TOKEN")
|
| 24 |
|
| 25 |
+
# Initialize pipeline for chat
|
| 26 |
+
pipeline_model = pipeline(
|
| 27 |
+
"text-generation",
|
| 28 |
+
model=model_path,
|
| 29 |
+
device_map="auto",
|
| 30 |
+
token=hf_token,
|
| 31 |
+
trust_remote_code=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
+
def generate_with_pipeline(messages, max_new_tokens=256, temperature=0.7, top_p=0.9, top_k=50, repetition_penalty=1.0):
|
| 35 |
+
"""Generate response using the pipeline with messages format"""
|
| 36 |
+
# Apply chat template
|
| 37 |
+
prompt = pipeline_model.tokenizer.apply_chat_template(
|
| 38 |
+
messages,
|
| 39 |
+
tokenize=False,
|
| 40 |
+
add_generation_prompt=True
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
outputs = pipeline_model(
|
| 44 |
+
prompt,
|
| 45 |
+
max_new_tokens=max_new_tokens,
|
| 46 |
+
temperature=temperature,
|
| 47 |
+
top_p=top_p,
|
| 48 |
+
top_k=top_k,
|
| 49 |
+
repetition_penalty=repetition_penalty,
|
| 50 |
+
do_sample=True,
|
| 51 |
+
return_full_text=False
|
| 52 |
+
)
|
| 53 |
+
return outputs[0]["generated_text"]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@spaces.GPU()
|
| 57 |
+
def generate_response(message, history, max_new_tokens, temperature, top_p, top_k, repetition_penalty):
|
| 58 |
+
"""
|
| 59 |
+
Generate response with full conversation history
|
| 60 |
+
|
| 61 |
+
Args:
|
| 62 |
+
message: Current user message (dict with 'text' key when type="messages")
|
| 63 |
+
history: List of previous messages (already in correct format for type="messages")
|
| 64 |
+
max_new_tokens, temperature, top_p, top_k, repetition_penalty: Generation parameters
|
| 65 |
+
"""
|
| 66 |
+
try:
|
| 67 |
+
# Build messages list starting with system prompt
|
| 68 |
+
messages = [{"role": "system", "content": DEFAULT_SYSTEM_PROMPT}]
|
| 69 |
+
|
| 70 |
+
# Add conversation history
|
| 71 |
+
# When type="messages", history is a list of message dicts with 'role' and 'content'
|
| 72 |
+
if history:
|
| 73 |
+
for msg in history:
|
| 74 |
+
if isinstance(msg, dict) and 'role' in msg and 'content' in msg:
|
| 75 |
+
messages.append({"role": msg['role'], "content": msg['content']})
|
| 76 |
+
|
| 77 |
+
# Add current user message
|
| 78 |
+
if isinstance(message, dict):
|
| 79 |
+
current_message = message.get("text", "") or message.get("content", "")
|
| 80 |
+
else:
|
| 81 |
+
current_message = str(message)
|
| 82 |
+
|
| 83 |
+
messages.append({"role": "user", "content": current_message})
|
| 84 |
+
|
| 85 |
+
# Debug: print messages structure
|
| 86 |
+
print(f"Messages sent to model: {len(messages)} messages")
|
| 87 |
+
|
| 88 |
+
# Generate response
|
| 89 |
+
response = generate_with_pipeline(
|
| 90 |
+
messages,
|
| 91 |
+
max_new_tokens=max_new_tokens,
|
| 92 |
+
temperature=temperature,
|
| 93 |
+
top_p=top_p,
|
| 94 |
+
top_k=top_k,
|
| 95 |
+
repetition_penalty=repetition_penalty
|
| 96 |
+
)
|
| 97 |
|
| 98 |
+
if not response or response.strip() == "":
|
| 99 |
+
response = "أهلاً! أنا أليكس مساعد خدمة العملاء. كيف أقدر أساعدك اليوم؟"
|
| 100 |
+
|
| 101 |
+
return response
|
| 102 |
+
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(f"Error in generate_response: {e}")
|
| 105 |
+
import traceback
|
| 106 |
+
print(traceback.format_exc())
|
| 107 |
+
return "عذراً، حدث خطأ. يرجى المحاولة مرة أخرى."
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# Create Gradio interface
|
| 111 |
+
demo = gr.ChatInterface(
|
| 112 |
+
fn=generate_response,
|
| 113 |
+
additional_inputs=[
|
| 114 |
+
gr.Slider(label="الحد الأقصى للكلمات الجديدة", minimum=64, maximum=4096, step=1, value=2048),
|
| 115 |
+
gr.Slider(label="درجة الحرارة", minimum=0.1, maximum=2.0, step=0.1, value=0.7),
|
| 116 |
+
gr.Slider(label="Top-p", minimum=0.05, maximum=1.0, step=0.05, value=0.9),
|
| 117 |
+
gr.Slider(label="Top-k", minimum=1, maximum=100, step=1, value=50),
|
| 118 |
+
gr.Slider(label="عقوبة التكرار", minimum=1.0, maximum=2.0, step=0.05, value=1.0)
|
| 119 |
+
],
|
| 120 |
+
examples=[
|
| 121 |
+
["النت عندي معطل من الصبح، تقدر تساعدني؟"],
|
| 122 |
+
["عندي مشكلة بالاتصال بالواي فاي"],
|
| 123 |
+
["شنو الباقات المتوفرة عندكم؟"],
|
| 124 |
+
["كيف أعيد ضبط الجهاز؟"],
|
| 125 |
+
["My device is not working properly"],
|
| 126 |
+
],
|
| 127 |
+
cache_examples=False,
|
| 128 |
+
type="messages",
|
| 129 |
+
title="دعم عملاء TechSolutions - مساعد أليكس (العراقي)",
|
| 130 |
+
description="""🤖 مساعد خدمة عملاء ذكي لـ TechSolutions
|
| 131 |
+
|
| 132 |
+
✨ المميزات:
|
| 133 |
+
- 🌐 دعم ثنائي اللغة (عربي وإنجليزي)
|
| 134 |
+
- 💬 لهجة محادثة طبيعية
|
| 135 |
+
- 🔧 دعم فني واستكشاف الأخطاء
|
| 136 |
+
- 📋 معلومات الخدمات والإرشاد
|
| 137 |
+
- 🧠 **يتذكر المحادثة السابقة** - يمكنك الرجوع للمواضيع السابقة
|
| 138 |
+
- 🎯 مدعوم بـ موديل Unsloth Meta-Llama-3.1-8B-Instruct-bnb-4bit
|
| 139 |
+
|
| 140 |
+
احجي مع أليكس لحل مشاكلك التقنية، استفسر عن الخدمات، أو احصل على معلومات المنتجات.""",
|
| 141 |
+
fill_height=True,
|
| 142 |
+
textbox=gr.Textbox(
|
| 143 |
+
label="اكتب رسالتك هنا",
|
| 144 |
+
placeholder="مثال: عندي مشكلة بالجهاز..."
|
| 145 |
+
),
|
| 146 |
+
stop_btn="إيقاف التوليد",
|
| 147 |
+
multimodal=False,
|
| 148 |
+
theme=gr.themes.Soft()
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
demo.launch()
|