Spaces:
Sleeping
Sleeping
Delete app0001.py
Browse files- app0001.py +0 -89
app0001.py
DELETED
|
@@ -1,89 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import openai
|
| 3 |
-
import os
|
| 4 |
-
from utils import (
|
| 5 |
-
analyze_pdf_files,
|
| 6 |
-
extract_text_and_vectors,
|
| 7 |
-
search_similar_content,
|
| 8 |
-
format_response,
|
| 9 |
-
log_debug_info,
|
| 10 |
-
get_uploaded_files
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
# گرفتن کلید API از محیط (برای Hugging Face Spaces)
|
| 14 |
-
openai.api_key = os.getenv("API_Key")
|
| 15 |
-
|
| 16 |
-
# تحلیل هوشمند با ChatGPT
|
| 17 |
-
def analyze_with_chatgpt(user_input):
|
| 18 |
-
prompt = f"""
|
| 19 |
-
کاربر نوشته: "{user_input}"
|
| 20 |
-
لطفاً نوع پروژه، آیتمهای موردنیاز، و اگر فرمولی هست رو تحلیل کن.
|
| 21 |
-
پاسخ را به صورت JSON فارسی و مختصر ارائه بده.
|
| 22 |
-
"""
|
| 23 |
-
try:
|
| 24 |
-
response = openai.ChatCompletion.create(
|
| 25 |
-
model="gpt-4",
|
| 26 |
-
messages=[{"role": "user", "content": prompt}],
|
| 27 |
-
temperature=0.2,
|
| 28 |
-
max_tokens=500
|
| 29 |
-
)
|
| 30 |
-
return response.choices[0].message.content.strip()
|
| 31 |
-
except Exception as e:
|
| 32 |
-
return f"❗ خطا در تحلیل ChatGPT: {e}"
|
| 33 |
-
|
| 34 |
-
# رابط اصلی گریدیو
|
| 35 |
-
def create_ui():
|
| 36 |
-
with gr.Blocks(title="دستیار هوش مصنوعی تحلیل اسناد برق") as demo:
|
| 37 |
-
gr.Markdown("## 📁 بارگذاری فایل یا ورود پرسش برای تحلیل")
|
| 38 |
-
|
| 39 |
-
with gr.Row():
|
| 40 |
-
with gr.Column():
|
| 41 |
-
file_input = gr.File(label="آپلود فایلهای PDF", file_types=[".pdf"], file_count="multiple")
|
| 42 |
-
user_query = gr.Textbox(label="سوال یا توضیح پروژه", placeholder="مثلاً: برای اجرای برق فشار ضعیف در یک خیابان روستایی...")
|
| 43 |
-
|
| 44 |
-
submit_btn = gr.Button("تحلیل و نمایش پاسخ")
|
| 45 |
-
|
| 46 |
-
with gr.Column():
|
| 47 |
-
chatgpt_output = gr.Textbox(label="🧠 تحلیل ChatGPT", lines=8, interactive=False)
|
| 48 |
-
final_output = gr.Textbox(label="📌 پاسخ نهایی بر اساس اسناد", lines=15, interactive=False, show_copy_button=True)
|
| 49 |
-
|
| 50 |
-
# دکمه دیباگ
|
| 51 |
-
with gr.Accordion("🔍 بررسی اطلاعات ذخیرهشده", open=False):
|
| 52 |
-
debug_btn = gr.Button("نمایش نمونه اطلاعات")
|
| 53 |
-
debug_output = gr.Textbox(label="📄 خروجی دیباگ", lines=10)
|
| 54 |
-
|
| 55 |
-
# عملکرد اصلی دکمه تحلیل
|
| 56 |
-
def handle_analysis(files, query):
|
| 57 |
-
# مرحله ۱: تحلیل ChatGPT
|
| 58 |
-
chatgpt_analysis = analyze_with_chatgpt(query)
|
| 59 |
-
|
| 60 |
-
# مرحله ۲: تحلیل فایلها
|
| 61 |
-
extracted_data = extract_text_and_vectors(files)
|
| 62 |
-
|
| 63 |
-
# مرحله ۳: جستجو و استخراج پاسخ
|
| 64 |
-
search_results = search_similar_content(query, extracted_data)
|
| 65 |
-
|
| 66 |
-
# مرحله ۴: ساخت خروجی
|
| 67 |
-
response = format_response(search_results)
|
| 68 |
-
|
| 69 |
-
return chatgpt_analysis, response
|
| 70 |
-
|
| 71 |
-
submit_btn.click(
|
| 72 |
-
fn=handle_analysis,
|
| 73 |
-
inputs=[file_input, user_query],
|
| 74 |
-
outputs=[chatgpt_output, final_output]
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
# دکمه دیباگ
|
| 78 |
-
debug_btn.click(
|
| 79 |
-
fn=log_debug_info,
|
| 80 |
-
inputs=[],
|
| 81 |
-
outputs=[debug_output]
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
return demo
|
| 85 |
-
|
| 86 |
-
# اجرای رابط
|
| 87 |
-
if __name__ == "__main__":
|
| 88 |
-
demo = create_ui()
|
| 89 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|