Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
-
# گرفتن کلید API از محیط (Hugging Face Spaces)
|
| 14 |
-
openai.api_key = os.getenv("API_Key")
|
| 15 |
-
|
| 16 |
-
def analyze_with_chatgpt(user_input):
|
| 17 |
-
prompt = f"""
|
| 18 |
-
کاربر نوشته: "{user_input}"
|
| 19 |
-
لطفاً نوع پروژه، آیتمهای موردنیاز، و اگر فرمولی هست رو تحلیل کن.
|
| 20 |
-
پاسخ را به صورت JSON فارسی و مختصر ارائه بده.
|
| 21 |
-
"""
|
| 22 |
-
try:
|
| 23 |
-
response = openai.ChatCompletion.create(
|
| 24 |
-
model="gpt-4",
|
| 25 |
-
messages=[{"role": "user", "content": prompt}],
|
| 26 |
-
temperature=0.2,
|
| 27 |
-
max_tokens=500
|
| 28 |
-
)
|
| 29 |
-
return response.choices[0].message.content.strip()
|
| 30 |
-
except Exception as e:
|
| 31 |
-
return f"❗ خطا در تحلیل ChatGPT: {e}"
|
| 32 |
-
|
| 33 |
-
def create_ui():
|
| 34 |
-
with gr.Blocks(title="دستیار هوش مصنوعی برآورد مصالح برق") as demo:
|
| 35 |
-
gr.Markdown("## ⚡ تحلیل متنی و برآورد مصالح پروژههای برق")
|
| 36 |
-
|
| 37 |
-
with gr.Row():
|
| 38 |
-
with gr.Column():
|
| 39 |
-
file_input = gr.File(label="آپلود فایل اکسل یا PDF", file_types=[".pdf", ".xlsx"], file_count="multiple")
|
| 40 |
-
user_query = gr.Textbox(label="توضیح پروژه یا شرط", placeholder="مثلاً: نصب ترانس 75 کیلو ولت آمپر با آرایش یکطرفه روی پایه فلزی...")
|
| 41 |
-
|
| 42 |
-
submit_btn = gr.Button("تحلیل و نمایش خروجی")
|
| 43 |
-
|
| 44 |
-
with gr.Column():
|
| 45 |
-
chatgpt_output = gr.Textbox(label="🧠 تحلیل اولیه ChatGPT", lines=8, interactive=False)
|
| 46 |
-
final_output = gr.Textbox(label="📌 خروجی نهایی بر اساس فایلها و شروط", lines=15, interactive=False, show_copy_button=True)
|
| 47 |
-
|
| 48 |
-
with gr.Accordion("🔍 مشاهده اطلاعات دیباگ", open=False):
|
| 49 |
-
debug_btn = gr.Button("نمایش اطلاعات ذخیرهشده")
|
| 50 |
-
debug_output = gr.Textbox(label="📄 خروجی دیباگ", lines=10)
|
| 51 |
-
|
| 52 |
-
def handle_analysis(files, query):
|
| 53 |
-
chatgpt_analysis = analyze_with_chatgpt(query)
|
| 54 |
-
extracted_data = extract_text_and_vectors(files)
|
| 55 |
-
search_results = search_similar_content(query, extracted_data)
|
| 56 |
-
response = format_response(search_results)
|
| 57 |
-
return chatgpt_analysis, response
|
| 58 |
-
|
| 59 |
-
submit_btn.click(
|
| 60 |
-
fn=handle_analysis,
|
| 61 |
-
inputs=[file_input, user_query],
|
| 62 |
-
outputs=[chatgpt_output, final_output]
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
debug_btn.click(
|
| 66 |
-
fn=log_debug_info,
|
| 67 |
-
inputs=[],
|
| 68 |
-
outputs=[debug_output]
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
return demo
|
| 72 |
-
|
| 73 |
if __name__ == "__main__":
|
| 74 |
-
demo = create_ui()
|
| 75 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from utils import load_material_db, filter_items
|
| 3 |
+
|
| 4 |
+
# بارگذاری پایگاه داده از فایل json
|
| 5 |
+
material_db = load_material_db()
|
| 6 |
+
|
| 7 |
+
def analyze_description(description, pole_height, pole_power, conductor_size):
|
| 8 |
+
# در این نسخه اول، صرفاً براساس ویژگیهای پایه فیلتر میکنیم
|
| 9 |
+
filtered_items = filter_items(material_db, pole_height, pole_power, conductor_size)
|
| 10 |
+
|
| 11 |
+
if not filtered_items:
|
| 12 |
+
return "هیچ کالایی با این مشخصات یافت نشد."
|
| 13 |
+
|
| 14 |
+
# ساخت جدول خروجی
|
| 15 |
+
output_lines = ["**کالاهای پیشنهادی:**\n"]
|
| 16 |
+
for item in filtered_items:
|
| 17 |
+
output_lines.append(f"- 📦 {item['item_name']} | 📏 {item['unit']} | 🔢 {item['quantity']} | کد فهرست بها: `{item['item_code']}`")
|
| 18 |
+
|
| 19 |
+
return "\n".join(output_lines)
|
| 20 |
+
|
| 21 |
+
# رابط کاربری Gradio
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=analyze_description,
|
| 24 |
+
inputs=[
|
| 25 |
+
gr.Textbox(label="توصیف پروژه (برای نسخههای بعدی استفاده میشود)"),
|
| 26 |
+
gr.Textbox(label="ارتفاع پایه (مثلاً 12)"),
|
| 27 |
+
gr.Textbox(label="توان پایه (مثلاً 800)"),
|
| 28 |
+
gr.Textbox(label="سایز هادی (مثلاً 120)")
|
| 29 |
+
],
|
| 30 |
+
outputs=gr.Markdown(label="نتیجه تحلیل براوردی"),
|
| 31 |
+
title="تحلیل براورد بر اساس توصیف و شرایط پایه",
|
| 32 |
+
description="نسخه اولیه برای نمایش کالاهای مرتبط از روی فایل اکسل و شرایط پروژه"
|
| 33 |
)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if __name__ == "__main__":
|
|
|
|
| 36 |
demo.launch()
|