Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
import tempfile
|
|
@@ -577,17 +577,27 @@ def execute_code_with_retry(code: str, max_attempts: int = 3) -> Tuple[bool, str
|
|
| 577 |
|
| 578 |
return False, "Max attempts reached", None
|
| 579 |
|
| 580 |
-
def
|
| 581 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 582 |
try:
|
| 583 |
if not instruction.strip():
|
| 584 |
return "لطفاً دستور را وارد کنید. (فایلها و لینکها اختیاری هستند)", None
|
| 585 |
|
| 586 |
-
file_paths =
|
| 587 |
-
|
| 588 |
-
# Handle uploaded files
|
| 589 |
-
if files:
|
| 590 |
-
file_paths = [f.name for f in files]
|
| 591 |
|
| 592 |
# Handle URLs: download to temp dir
|
| 593 |
if urls_input and urls_input.strip():
|
|
@@ -601,8 +611,6 @@ def process_request(instruction, files, urls_input):
|
|
| 601 |
file_paths.extend(downloaded_paths)
|
| 602 |
print(f"Downloaded {len(downloaded_paths)} files from URLs")
|
| 603 |
|
| 604 |
-
# Clean up note: Temp dirs will be cleaned on shutdown or manually if needed
|
| 605 |
-
|
| 606 |
# Track errors for learning
|
| 607 |
previous_errors = []
|
| 608 |
generated_codes = []
|
|
@@ -665,11 +673,13 @@ def process_request(instruction, files, urls_input):
|
|
| 665 |
print(error_msg)
|
| 666 |
return error_msg, None
|
| 667 |
|
| 668 |
-
#
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
# 🤖 AI File Processor - Self Correcting Edition
|
| 672 |
|
|
|
|
|
|
|
|
|
|
| 673 |
این سیستم میتواند:
|
| 674 |
- کد Python تولید کند
|
| 675 |
- خطاها را تشخیص و تحلیل کند
|
|
@@ -684,40 +694,55 @@ with gr.Blocks(title="AI File Processor - Self Correcting") as demo:
|
|
| 684 |
**نکته:** میتوانید فایلها را آپلود کنید یا لینکهای فایل را (جدا شده با کاما) وارد کنید.
|
| 685 |
""")
|
| 686 |
|
| 687 |
-
|
| 688 |
-
|
|
|
|
|
|
|
| 689 |
label="دستور",
|
| 690 |
-
|
| 691 |
placeholder="مثال: یک نمودار دایرهای از دادههای فروش بکش",
|
| 692 |
-
|
| 693 |
)
|
| 694 |
|
| 695 |
-
with
|
| 696 |
-
|
| 697 |
-
|
| 698 |
label="لینک فایلها (جدا با کاما، اختیاری)",
|
| 699 |
placeholder="https://example.com/file1.csv, https://example.com/file2.jpg",
|
| 700 |
-
|
| 701 |
)
|
| 702 |
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
["این فایل را به فرمت JSON تبدیل کن", None, "https://example.com/sample.csv"],
|
| 716 |
-
],
|
| 717 |
-
inputs=[instruction, files, urls],
|
| 718 |
-
)
|
| 719 |
|
| 720 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 721 |
|
| 722 |
if __name__ == "__main__":
|
| 723 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
import tempfile
|
|
|
|
| 577 |
|
| 578 |
return False, "Max attempts reached", None
|
| 579 |
|
| 580 |
+
def save_uploaded_files(uploaded_files):
|
| 581 |
+
"""Save uploaded Streamlit files to temp dir and return paths."""
|
| 582 |
+
if not uploaded_files:
|
| 583 |
+
return []
|
| 584 |
+
temp_dir = tempfile.mkdtemp(prefix='uploaded_files_')
|
| 585 |
+
file_paths = []
|
| 586 |
+
for uploaded_file in uploaded_files:
|
| 587 |
+
if uploaded_file is not None:
|
| 588 |
+
file_path = os.path.join(temp_dir, uploaded_file.name or 'unnamed_file')
|
| 589 |
+
with open(file_path, 'wb') as f:
|
| 590 |
+
f.write(uploaded_file.getbuffer())
|
| 591 |
+
file_paths.append(file_path)
|
| 592 |
+
return file_paths
|
| 593 |
+
|
| 594 |
+
def process_request(instruction, uploaded_files, urls_input):
|
| 595 |
+
"""Main processing function with self-correction, supporting URLs. Adapted for Streamlit."""
|
| 596 |
try:
|
| 597 |
if not instruction.strip():
|
| 598 |
return "لطفاً دستور را وارد کنید. (فایلها و لینکها اختیاری هستند)", None
|
| 599 |
|
| 600 |
+
file_paths = save_uploaded_files(uploaded_files)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 601 |
|
| 602 |
# Handle URLs: download to temp dir
|
| 603 |
if urls_input and urls_input.strip():
|
|
|
|
| 611 |
file_paths.extend(downloaded_paths)
|
| 612 |
print(f"Downloaded {len(downloaded_paths)} files from URLs")
|
| 613 |
|
|
|
|
|
|
|
| 614 |
# Track errors for learning
|
| 615 |
previous_errors = []
|
| 616 |
generated_codes = []
|
|
|
|
| 673 |
print(error_msg)
|
| 674 |
return error_msg, None
|
| 675 |
|
| 676 |
+
# Streamlit Interface
|
| 677 |
+
def main():
|
| 678 |
+
st.set_page_config(page_title="AI File Processor - Self Correcting", layout="wide")
|
|
|
|
| 679 |
|
| 680 |
+
st.title("🤖 AI File Processor - Self Correcting Edition")
|
| 681 |
+
|
| 682 |
+
st.markdown("""
|
| 683 |
این سیستم میتواند:
|
| 684 |
- کد Python تولید کند
|
| 685 |
- خطاها را تشخیص و تحلیل کند
|
|
|
|
| 694 |
**نکته:** میتوانید فایلها را آپلود کنید یا لینکهای فایل را (جدا شده با کاما) وارد کنید.
|
| 695 |
""")
|
| 696 |
|
| 697 |
+
col1, col2 = st.columns(2)
|
| 698 |
+
|
| 699 |
+
with col1:
|
| 700 |
+
instruction = st.text_area(
|
| 701 |
label="دستور",
|
| 702 |
+
height=100,
|
| 703 |
placeholder="مثال: یک نمودار دایرهای از دادههای فروش بکش",
|
| 704 |
+
help="دستور خود را به فارسی یا انگلیسی وارد کنید."
|
| 705 |
)
|
| 706 |
|
| 707 |
+
with col2:
|
| 708 |
+
uploaded_files = st.file_uploader("فایلهای آپلود شده (اختیاری)", type=['csv', 'xlsx', 'jpg', 'png', 'txt'], accept_multiple_files=True)
|
| 709 |
+
urls_input = st.text_input(
|
| 710 |
label="لینک فایلها (جدا با کاما، اختیاری)",
|
| 711 |
placeholder="https://example.com/file1.csv, https://example.com/file2.jpg",
|
| 712 |
+
help="لینکها دانلود خواهند شد."
|
| 713 |
)
|
| 714 |
|
| 715 |
+
# Examples as selectbox
|
| 716 |
+
examples = [
|
| 717 |
+
("یک فایل اکسل با 100 محصول فروشگاهی شامل نام، قیمت و موجودی بساز", None, None),
|
| 718 |
+
("یک نمودار میلهای از دادههای تصادفی رسم کن", None, None),
|
| 719 |
+
("یک تصویر 500x500 پیکسل با رنگهای تصادفی بساز", None, None),
|
| 720 |
+
("این فایل را به فرمت JSON تبدیل کن", None, "https://example.com/sample.csv"),
|
| 721 |
+
]
|
| 722 |
+
|
| 723 |
+
selected_example = st.selectbox("انتخاب مثال:", ["هیچکدام"] + [ex[0] for ex in examples])
|
| 724 |
+
if selected_example != "هیچکدام":
|
| 725 |
+
idx = [ex[0] for ex in examples].index(selected_example)
|
| 726 |
+
instruction, _, urls_input = examples[idx] # Note: files can't be pre-set easily in Streamlit
|
|
|
|
|
|
|
|
|
|
|
|
|
| 727 |
|
| 728 |
+
if st.button("🚀 اجرا", type="primary"):
|
| 729 |
+
with st.spinner("در حال پردازش..."):
|
| 730 |
+
result_text, output_file_path = process_request(instruction, uploaded_files, urls_input)
|
| 731 |
+
|
| 732 |
+
st.subheader("نتیجه")
|
| 733 |
+
st.text_area("خروجی:", value=result_text, height=300, key="result")
|
| 734 |
+
|
| 735 |
+
if output_file_path and os.path.exists(output_file_path):
|
| 736 |
+
with open(output_file_path, "rb") as file:
|
| 737 |
+
st.download_button(
|
| 738 |
+
label="دانلود فایل خروجی",
|
| 739 |
+
data=file.read(),
|
| 740 |
+
file_name=os.path.basename(output_file_path),
|
| 741 |
+
mime="application/octet-stream"
|
| 742 |
+
)
|
| 743 |
+
# Clean up after download (optional, but for Spaces it's fine to leave temp)
|
| 744 |
+
else:
|
| 745 |
+
st.info("هیچ فایل خروجی تولید نشد.")
|
| 746 |
|
| 747 |
if __name__ == "__main__":
|
| 748 |
+
main()
|