Spaces:
Sleeping
Sleeping
| import os | |
| import base64 | |
| from io import BytesIO | |
| from PIL import Image | |
| import gradio as gr | |
| from openai import AzureOpenAI | |
| import json | |
| from dotenv import load_dotenv, find_dotenv | |
| endpoint = os.getenv("ENDPOINT_URL", "https://lahja-dev-resource.openai.azure.com/openai/deployments/Wasm-V1/chat/completions?api-version=2025-01-01-preview") | |
| deployment = os.getenv("DEPLOYMENT_NAME", "Wasm-V1") | |
| subscription_key ="4AwsIf87cyBIgaJVsy0phWUQdZFcbrJxpQBDQNzL4xjcP2MFzrrYJQQJ99BIACHYHv6XJ3w3AAAAACOGYrzM" | |
| # Configure Azure OpenAI client | |
| client = AzureOpenAI( | |
| azure_endpoint=endpoint, # e.g. https://your-resource-name.openai.azure.com/ | |
| api_key=subscription_key, # your Azure API key | |
| api_version="2025-01-01-preview" | |
| ) | |
| def get_completion(prompt): | |
| """ | |
| Helper function to get model completion from Azure OpenAI. | |
| """ | |
| response = client.chat.completions.create( | |
| model=deployment, # Use the deployment name in Azure | |
| messages=[{"role": "user", "content": prompt}], | |
| temperature=0 # Degree of randomness | |
| ) | |
| return response.choices[0].message.content | |
| AZURE_ENDPOINT1 = "https://lahja-dev-resource.openai.azure.com/openai/deployments/Wasm-V1/chat/completions?api-version=2025-01-01-preview" | |
| AZURE_API_KEY1 = "4AwsIf87cyBIgaJVsy0phWUQdZFcbrJxpQBDQNzL4xjcP2MFzrrYJQQJ99BIACHYHv6XJ3w3AAAAACOGYrzM" | |
| MODEL_NAME1 = "Wasm-Dash" | |
| # Configure Azure OpenAI client | |
| clientdach = AzureOpenAI( | |
| azure_endpoint=AZURE_ENDPOINT1, # e.g. https://your-resource-name.openai.azure.com/ | |
| api_key=AZURE_API_KEY1, # your Azure API key | |
| api_version="2025-01-01-preview" | |
| ) | |
| def get_completionDach(prompt): | |
| """ | |
| Helper function to get model completion from Azure OpenAI. | |
| """ | |
| response = clientdach.chat.completions.create( | |
| model=MODEL_NAME1, # Use the deployment name in Azure | |
| messages=[{"role": "user", "content": prompt}], | |
| temperature=0 # Degree of randomness | |
| ) | |
| return response.choices[0].message.content | |
| # --------------------------------------------------------- | |
| # إعداد عميل Azure OpenAI | |
| # --------------------------------------------------------- | |
| endpoint = os.getenv("ENDPOINT_URL", "https://lahja-dev-resource.openai.azure.com/openai/deployments/Wasm-V1/chat/completions?api-version=2025-01-01-preview") | |
| deployment = os.getenv("DEPLOYMENT_NAME", "Wasm-V1") | |
| subscription_key = "4AwsIf87cyBIgaJVsy0phWUQdZFcbrJxpQBDQNzL4xjcP2MFzrrYJQQJ99BIACHYHv6XJ3w3AAAAACOGYrzM" | |
| client = AzureOpenAI( | |
| azure_endpoint=endpoint, | |
| api_key=subscription_key, | |
| api_version="2025-01-01-preview" | |
| ) | |
| # --------------------------------------------------------- | |
| # دالة للحصول على رد النموذج | |
| # --------------------------------------------------------- | |
| def get_completion(prompt): | |
| response = client.chat.completions.create( | |
| model=deployment, | |
| messages=prompt, | |
| temperature=0 | |
| ) | |
| return response.choices[0].message.content | |
| # --------------------------------------------------------- | |
| # دالة لتحويل JSON إلى HTML | |
| # --------------------------------------------------------- | |
| def render_tree_html(tree_json_str): | |
| prompt = f""" | |
| أنت خبير في تصميم صفحات HTML جذابة وتفاعلية. | |
| لديك بيانات شجرة في السعودية بصيغة JSON كما يلي: | |
| {tree_json_str} | |
| مهمتك: | |
| 1. أنشئ صفحة HTML جذابة تعرض جميع المعلومات بشكل منسق. | |
| 2. حول الحقول الرقمية (مثل نسبة إنتاج الأكسجين وامتصاص CO₂) إلى رسوم بيانية بسيطة باستخدام CSS أو SVG. | |
| 3. أضف صورة توضيحية للشجرة. | |
| 4. اجعل الصفحة تحتوي على عناوين واضحة لكل قسم. | |
| 5. أجب فقط بكود HTML كامل جاهز للعرض في المتصفح، بدون أي شروحات إضافية. | |
| """ | |
| html=get_completionDach(prompt) | |
| return html | |
| # --------------------------------------------------------- | |
| # دالة لمعالجة الصورة أو الفيديو | |
| # --------------------------------------------------------- | |
| def analyze_tree_from_file(file_obj): | |
| image_message = [] | |
| if file_obj is not None: | |
| with open(file_obj.name, "rb") as f: | |
| file_data = base64.b64encode(f.read()).decode("utf-8") | |
| # البرومبت التحليلي | |
| prompt = f""" | |
| أنت خبير زراعي متخصص في الأشجار والنباتات في المملكة العربية السعودية. | |
| مهمتك هي تحليل هذه الشجرة في الصورة وتحديد معلوماتها الزراعية ضمن الظروف السعودية فقط. | |
| أرجع النتيجة في تنسيق JSON منظم يحتوي على الحقول التالية: | |
| {{ | |
| "اسم_الشجرة": "", | |
| "الاسم_العلمي": "", | |
| " بالميه نسبة_إنتاج_الأكسجين": "", | |
| "الوصف_العام": "", | |
| "الفوائد": [], | |
| "المشاكل_الشائعة": [], | |
| "من_ايش_تعاني_الشجرة": [], | |
| "البيئة_المناسبة": "", | |
| "نوع_التربة_المناسبة": "", | |
| "الطقس_المناسب": "", | |
| "فصل_النمو": "", | |
| "نسبة_امتصاص_ثاني_أكسيد_الكربون": "", | |
| "استخدامات_زراعية_وصناعية": [], | |
| "المناطق_المناسبة_لزرعها_في_السعودية": [], | |
| "ملاحظات": "" | |
| }} | |
| استخدم معرفتك العلمية لتقدير البيانات إذا لم تكن واضحة في الصورة. | |
| أجب فقط بصيغة JSON. لا تضف أي شرح إضافي قبل أو بعد الـ JSON. | |
| """ | |
| messagesr = [ | |
| {"role": "system", "content": "أنت خبير زراعي في النباتات والأشجار في المملكة العربية السعودية."}, | |
| {"role": "user", "content": [ | |
| {"type": "text", "text": prompt}, | |
| *image_message | |
| ]} | |
| ] | |
| messages = [ | |
| { | |
| "role": "user", | |
| "content": [ | |
| {"type": "text", "text": prompt}, | |
| { | |
| "type": "image_url", | |
| "image_url": { | |
| "url": f"data:image/jpeg;base64,{file_data}" | |
| }, | |
| }, | |
| ], | |
| } | |
| ] | |
| tree_json_str = get_completion(messages) | |
| return render_tree_html(tree_json_str) | |
| iface = gr.Interface( | |
| fn=analyze_tree_from_file, | |
| inputs=gr.File(label="رفع صورة الشجرة أو الفيديو"), | |
| outputs=gr.HTML(), | |
| title="المحلل الزراعي للأشجار في السعودية", | |
| description="قم برفع صورة أو فيديو الشجرة لتحليلها واستخراج معلوماتها الزراعية وعرضها." | |
| ) | |
| iface.launch(debug=True,share=True) |