add file upload support
Browse filesFiles changed (2)
- app.py+44-3
- requirements.txt+3-1
2 unmodified lines34567891011355 unmodified lines3673683693703713723733743753763773783793803813823833843853863873883893903913923933673943953963973 unmodified lines40140240340440540640740840941041141241341451 unmodified lines4664674684694704714723 unmodified lines47647747847948048148248348448548671 unmodified lines5585595605205615625635235645655665672 unmodified linesfrom http import HTTPStatusfrom typing import Dict, List, Optional, Tupleimport base64import mimetypesimport PyPDF2import docx
import gradio as grfrom huggingface_hub import InferenceClient355 unmodified lines # Return the first demo description as fallback return DEMO_LIST[0]['description']
def extract_text_from_file(file_path): if not file_path: return "" mime, _ = mimetypes.guess_type(file_path) ext = os.path.splitext(file_path)[1].lower() try: if ext == ".pdf": with open(file_path, "rb") as f: reader = PyPDF2.PdfReader(f) return "\n".join(page.extract_text() or "" for page in reader.pages) elif ext in [".txt", ".md"]: with open(file_path, "r", encoding="utf-8") as f: return f.read() elif ext == ".csv": with open(file_path, "r", encoding="utf-8") as f: return f.read() elif ext == ".docx": doc = docx.Document(file_path) return "\n".join([para.text for para in doc.paragraphs]) else: return "" except Exception as e: return f"Error extracting text: {e}"
def generation_code(query: Optional[str], image: Optional[gr.Image], _setting: Dict[str, str], _history: Optional[History], _current_model: Dict, enable_search: bool = False):def generation_code(query: Optional[str], image: Optional[gr.Image], file: Optional[str], _setting: Dict[str, str], _history: Optional[History], _current_model: Dict, enable_search: bool = False): if query is None: query = '' if _history is None:3 unmodified lines system_prompt = SystemPromptWithSearch if enable_search else _setting['system'] messages = history_to_messages(_history, system_prompt) # Extract file text and append to query if file is present file_text = "" if file: file_text = extract_text_from_file(file) if file_text: file_text = file_text[:5000] # Limit to 5000 chars for prompt size query = f"{query}\n\n[Reference file content below]\n{file_text}" # Enhance query with search if enabled enhanced_query = enhance_query_with_search(query, enable_search) 51 unmodified lines gr.Markdown("# AnyCoder\nAI-Powered Code Generator") gr.Markdown("""Describe your app or UI in plain English. Optionally upload a UI image (for ERNIE model). Click Generate to get code and preview.""") gr.Markdown("**Tip:** For best search results about people or entities, include details like profession, company, or location. Example: 'John Smith software engineer at Google.'") gr.Markdown("**Tip:** You can attach a file (PDF, TXT, DOCX, CSV, MD) to use as reference for your prompt, e.g. 'Summarize this PDF.'") input = gr.Textbox( label="Describe your application", placeholder="e.g., Create a todo app with add, delete, and mark as complete functionality",3 unmodified lines label="Upload UI design image (ERNIE-4.5-VL only)", visible=False ) file_input = gr.File( label="Attach a file (PDF, TXT, DOCX, CSV, MD)", file_types=[".pdf", ".txt", ".md", ".csv", ".docx"], visible=True ) with gr.Row(): btn = gr.Button("Generate", variant="primary", size="sm") clear_btn = gr.Button("Clear", variant="secondary", size="sm")71 unmodified lines # Event handlers btn.click( generation_code, inputs=[input, image_input, setting, history, current_model, search_toggle], inputs=[input, image_input, file_input, setting, history, current_model, search_toggle], outputs=[code_output, history, sandbox, status_indicator, history_output] ) clear_btn.click(clear_history, outputs=[history, history_output]) clear_btn.click(clear_history, outputs=[history, history_output, file_input])
if __name__ == "__main__": demo.queue(default_concurrency_limit=20).launch(ssr_mode=True, mcp_server=True)123345git+https://github.com/huggingface/huggingface_hub.gitgradio[oauth]tavily-pythontavily-pythonPyPDF2python-docxapp.py+44-3
requirements.txt+3-1