File size: 2,556 Bytes
3d201f0
76678f3
 
d412de0
3d201f0
 
 
76678f3
3d201f0
 
 
 
76678f3
 
3d201f0
76678f3
 
3d201f0
76678f3
 
 
 
 
 
 
 
 
 
3d201f0
 
76678f3
 
 
 
 
3d201f0
 
76678f3
 
3d201f0
76678f3
 
 
 
 
 
 
 
 
3d201f0
76678f3
 
3d201f0
 
76678f3
 
 
3d201f0
76678f3
 
 
 
 
 
 
3d201f0
76678f3
 
 
3d201f0
 
 
76678f3
 
 
 
3d201f0
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import gradio as gr
from groq import Groq

# القراءة الصحيحة لمفتاح Groq من متغير بيئة
# على Hugging Face Space: ضيف GROQ_API_KEY في Secrets
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))

SYSTEM_PROMPT = (
    "You are an expert in Premier League Fantasy. "
    "Provide structured and insightful responses to queries about players and coaches."
)

def respond(message, history, model, temperature, max_tokens):
    # نحول الهيستوري إلى تنسيق رسائل
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    for h in history:
        # h[0] = user; h[1] = assistant
        messages.append({"role": "user", "content": h[0]})
        if h[1]:
            messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    try:
        response = client.chat.completions.create(
            model=model,
            messages=messages,
            temperature=temperature,
            # الاسم المتوافق شائع الاستخدام
            max_tokens=int(max_tokens),
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"Error: {str(e)}"


# ChatInterface مع مدخلات إضافية (موديل/حرارة/حد أقصى للتوكنز)
demo = gr.ChatInterface(
    fn=respond,
    title="🎬 Premier League Fantasy Generator AI",
    description="Be the hero of the Week!",
    additional_inputs=[
        gr.Dropdown(
            choices=[
                "llama-3.3-70b-versatile",
                "llama-3.1-8b-instant",
            ],
            value="llama-3.3-70b-versatile",
            label="Model",
            info="Select the AI model to use",
        ),
        gr.Slider(
            minimum=0.0,
            maximum=2.0,
            value=0.9,
            step=0.1,
            label="Temperature",
            info="Controls randomness. Lower = more focused, Higher = more creative",
        ),
        gr.Slider(
            minimum=256,
            maximum=8192,
            value=2048,
            step=256,
            label="Max Tokens",
            info="Maximum length of the response",
        ),
    ],
    examples=[
        ["Who is the best player to pick this week?"],
        ["Which player should I captain this week?"],
        ["Give me the perfect team formation for this week."],
    ],
)

if __name__ == "__main__":
    # التيم بيتحدّد هنا (Gradio v6+)
    demo.launch(theme=gr.themes.Soft())