anaspro
commited on
Commit
·
bb3a512
1
Parent(s):
201bb0a
updatE
Browse files
app.py
CHANGED
|
@@ -1,152 +1,70 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
# Load
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"text
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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 |
-
|
|
|
|
|
|
| 1 |
+
Hugging Face's logo
|
| 2 |
+
Hugging Face
|
| 3 |
+
Models
|
| 4 |
+
Datasets
|
| 5 |
+
Spaces
|
| 6 |
+
Community
|
| 7 |
+
Docs
|
| 8 |
+
Pricing
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
Spaces:
|
| 12 |
+
EzioDevio
|
| 13 |
+
/
|
| 14 |
+
iraqi_dialect_demo
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
like
|
| 18 |
+
0
|
| 19 |
+
App
|
| 20 |
+
Files
|
| 21 |
+
Community
|
| 22 |
+
iraqi_dialect_demo
|
| 23 |
+
/
|
| 24 |
+
app.py
|
| 25 |
+
|
| 26 |
+
EzioDevio's picture
|
| 27 |
+
EzioDevio
|
| 28 |
+
Update app.py
|
| 29 |
+
de631e1
|
| 30 |
+
verified
|
| 31 |
+
12 months ago
|
| 32 |
+
raw
|
| 33 |
+
|
| 34 |
+
Copy download link
|
| 35 |
+
history
|
| 36 |
+
blame
|
| 37 |
+
contribute
|
| 38 |
+
delete
|
| 39 |
+
|
| 40 |
+
1.01 kB
|
| 41 |
import gradio as gr
|
| 42 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 43 |
+
|
| 44 |
+
# Load the model and tokenizer
|
| 45 |
+
model_path = "EzioDevio/iraqi_dialect_llm" # Use the Hugging Face Hub model path
|
| 46 |
+
|
| 47 |
+
# Load the model and tokenizer directly from Hugging Face
|
| 48 |
+
model = AutoModelForCausalLM.from_pretrained(model_path)
|
| 49 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 50 |
+
text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 51 |
+
|
| 52 |
+
# Define the text generation function
|
| 53 |
+
def generate_text(input_text):
|
| 54 |
+
generated = text_generator(input_text, max_length=50, num_return_sequences=1)
|
| 55 |
+
return generated[0]["generated_text"]
|
| 56 |
+
|
| 57 |
+
# Set up the Gradio interface
|
| 58 |
+
iface = gr.Interface(
|
| 59 |
+
fn=generate_text,
|
| 60 |
+
inputs="text",
|
| 61 |
+
outputs="text",
|
| 62 |
+
title="Iraqi Dialect Language Model",
|
| 63 |
+
description="Generate text in Iraqi Arabic dialect.",
|
| 64 |
+
examples=["شلونك اليوم؟", "وين رايح؟", "صباح الخير"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# Launch the app
|
| 68 |
if __name__ == "__main__":
|
| 69 |
+
iface.launch()
|
| 70 |
+
|