File size: 2,208 Bytes
4622616
c371866
 
 
 
 
 
 
4622616
c371866
 
 
 
 
 
 
4622616
c371866
 
 
 
4622616
c371866
 
 
 
4622616
c371866
 
 
4622616
c371866
4622616
 
c371866
4622616
c371866
 
4622616
 
 
 
c371866
 
 
 
 
 
 
 
 
 
4622616
 
 
 
c371866
 
 
 
4622616
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
# تثبيت المكتبات المطلوبة
!pip install gradio transformers accelerate bitsandbytes huggingface_hub

import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
from huggingface_hub import login


# 🔹 تحديد اسم النموذج
model_name = "google/gemma-2b-it"

# 🔹 تحميل التوكن
tokenizer = AutoTokenizer.from_pretrained(model_name)

# 🔹 تحميل النموذج مع تحسين استهلاك الذاكرة
quantization_config = BitsAndBytesConfig(load_in_8bit=True)  # تحميل النموذج بتنسيق 8-bit لتقليل استهلاك الذاكرة

model = AutoModelForCausalLM.from_pretrained(
    model_name,
    quantization_config=quantization_config,
    device_map="auto"  # توزيع الحمل بين GPU و RAM تلقائيًا
)



print(f"✅ تم رفع النموذج بنجاح! يمكنك الوصول إليه على: https://huggingface.co/{username}/{repo_name}")

# 🔹 دالة معالجة الأسئلة الخاصة بالسباحة
def swimming_coach_advice(question):
    prompt = f"User: {question}\nAI Coach:"

    # تحويل النص إلى أرقام
    inputs = tokenizer(prompt, return_tensors="pt").to("cuda")  # استخدام الـ GPU إذا كان متاحًا

    # توليد النص بطريقة فعالة
    output = model.generate(
        **inputs,
        max_length=150,  # تحديد الطول الأقصى للخروج
        temperature=0.7,  # ضبط العشوائية
        top_p=0.9,  # تحسين الاستجابة
        repetition_penalty=1.2  # تقليل التكرار
    )

    # تحويل المخرجات إلى نص
    response = tokenizer.decode(output[0], skip_special_tokens=True)

    return response

# 🔹 إنشاء واجهة Gradio
interface = gr.Interface(
    fn=swimming_coach_advice,
    inputs=gr.Textbox(label="Ask the AI Swimming Coach a question"),
    outputs=gr.Textbox(label="AI Coach Response"),
    title="AI Swimming Coach",
    description="Ask me anything about swimming techniques, training, or tips!"
)

# 🔹 تشغيل التطبيق
if __name__ == "__main__":
    interface.launch()