Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
SYSTEM_PROMPT =
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(message, history, model, temperature, max_tokens):
|
|
|
|
| 11 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 12 |
-
|
| 13 |
for h in history:
|
|
|
|
| 14 |
messages.append({"role": "user", "content": h[0]})
|
| 15 |
if h[1]:
|
| 16 |
messages.append({"role": "assistant", "content": h[1]})
|
| 17 |
-
|
| 18 |
messages.append({"role": "user", "content": message})
|
| 19 |
|
| 20 |
try:
|
|
@@ -22,16 +26,18 @@ def respond(message, history, model, temperature, max_tokens):
|
|
| 22 |
model=model,
|
| 23 |
messages=messages,
|
| 24 |
temperature=temperature,
|
| 25 |
-
|
|
|
|
| 26 |
)
|
| 27 |
return response.choices[0].message.content
|
| 28 |
except Exception as e:
|
| 29 |
return f"Error: {str(e)}"
|
| 30 |
|
| 31 |
-
|
|
|
|
| 32 |
demo = gr.ChatInterface(
|
| 33 |
fn=respond,
|
| 34 |
-
title="🎬
|
| 35 |
description="Be the hero of the Week!",
|
| 36 |
additional_inputs=[
|
| 37 |
gr.Dropdown(
|
|
@@ -41,15 +47,15 @@ demo = gr.ChatInterface(
|
|
| 41 |
],
|
| 42 |
value="llama-3.3-70b-versatile",
|
| 43 |
label="Model",
|
| 44 |
-
info="Select the AI model to use"
|
| 45 |
),
|
| 46 |
gr.Slider(
|
| 47 |
-
minimum=0,
|
| 48 |
-
maximum=2,
|
| 49 |
value=0.9,
|
| 50 |
step=0.1,
|
| 51 |
label="Temperature",
|
| 52 |
-
info="Controls randomness. Lower = more focused, Higher = more creative"
|
| 53 |
),
|
| 54 |
gr.Slider(
|
| 55 |
minimum=256,
|
|
@@ -57,16 +63,16 @@ demo = gr.ChatInterface(
|
|
| 57 |
value=2048,
|
| 58 |
step=256,
|
| 59 |
label="Max Tokens",
|
| 60 |
-
info="Maximum length of the response"
|
| 61 |
),
|
| 62 |
],
|
| 63 |
examples=[
|
| 64 |
-
["
|
| 65 |
-
["
|
| 66 |
-
["Give me the perfect
|
| 67 |
],
|
| 68 |
-
theme="soft",
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
|
|
|
| 4 |
|
| 5 |
+
# القراءة الصحيحة لمفتاح Groq من متغير بيئة
|
| 6 |
+
# على Hugging Face Space: ضيف GROQ_API_KEY في Secrets
|
| 7 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
+
SYSTEM_PROMPT = (
|
| 10 |
+
"You are an expert in Premier League Fantasy. "
|
| 11 |
+
"Provide structured and insightful responses to queries about players and coaches."
|
| 12 |
+
)
|
| 13 |
|
| 14 |
def respond(message, history, model, temperature, max_tokens):
|
| 15 |
+
# نحول الهيستوري إلى تنسيق رسائل
|
| 16 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
|
| 17 |
for h in history:
|
| 18 |
+
# h[0] = user; h[1] = assistant
|
| 19 |
messages.append({"role": "user", "content": h[0]})
|
| 20 |
if h[1]:
|
| 21 |
messages.append({"role": "assistant", "content": h[1]})
|
|
|
|
| 22 |
messages.append({"role": "user", "content": message})
|
| 23 |
|
| 24 |
try:
|
|
|
|
| 26 |
model=model,
|
| 27 |
messages=messages,
|
| 28 |
temperature=temperature,
|
| 29 |
+
# الاسم المتوافق شائع الاستخدام
|
| 30 |
+
max_tokens=int(max_tokens),
|
| 31 |
)
|
| 32 |
return response.choices[0].message.content
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error: {str(e)}"
|
| 35 |
|
| 36 |
+
|
| 37 |
+
# ChatInterface مع مدخلات إضافية (موديل/حرارة/حد أقصى للتوكنز)
|
| 38 |
demo = gr.ChatInterface(
|
| 39 |
fn=respond,
|
| 40 |
+
title="🎬 Premier League Fantasy Generator AI",
|
| 41 |
description="Be the hero of the Week!",
|
| 42 |
additional_inputs=[
|
| 43 |
gr.Dropdown(
|
|
|
|
| 47 |
],
|
| 48 |
value="llama-3.3-70b-versatile",
|
| 49 |
label="Model",
|
| 50 |
+
info="Select the AI model to use",
|
| 51 |
),
|
| 52 |
gr.Slider(
|
| 53 |
+
minimum=0.0,
|
| 54 |
+
maximum=2.0,
|
| 55 |
value=0.9,
|
| 56 |
step=0.1,
|
| 57 |
label="Temperature",
|
| 58 |
+
info="Controls randomness. Lower = more focused, Higher = more creative",
|
| 59 |
),
|
| 60 |
gr.Slider(
|
| 61 |
minimum=256,
|
|
|
|
| 63 |
value=2048,
|
| 64 |
step=256,
|
| 65 |
label="Max Tokens",
|
| 66 |
+
info="Maximum length of the response",
|
| 67 |
),
|
| 68 |
],
|
| 69 |
examples=[
|
| 70 |
+
["Who is the best player to pick this week?"],
|
| 71 |
+
["Which player should I captain this week?"],
|
| 72 |
+
["Give me the perfect team formation for this week."],
|
| 73 |
],
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
+
# التيم بيتحدّد هنا (Gradio v6+)
|
| 78 |
+
demo.launch(theme=gr.themes.Soft())
|