Spaces:
Sleeping
Sleeping
Download app.py from Ai1996/Data_entry321: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/spaces/Ai1996/Data_entry321/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ai1996/Data_entry321/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ai1996/Data_entry321/resolve/main/app.py
2.61 kB
| import os | |
| import gradio as gr | |
| import pandas as pd | |
| from docx import Document | |
| from fpdf import FPDF | |
| from groq import Groq | |
| # Initialize Groq client using API key from environment variable | |
| client = Groq(api_key=os.environ.get("GROQ_API_KEY")) | |
| def generate_table_from_text(text_prompt): | |
| response = client.chat.completions.create( | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": f"Convert this into a clean table with comma-separated values:\n{text_prompt}" | |
| } | |
| ], | |
| model="llama3-8b-8192" | |
| ) | |
| return response.choices[0].message.content | |
| def save_files(data_text): | |
| rows = [row.strip().split(",") for row in data_text.strip().split("\n")] | |
| headers = rows[0] | |
| records = rows[1:] | |
| df = pd.DataFrame(records, columns=headers) | |
| # Save Excel | |
| excel_path = "output.xlsx" | |
| df.to_excel(excel_path, index=False) | |
| # Save Word | |
| doc = Document() | |
| doc.add_heading("Structured Data", 0) | |
| table = doc.add_table(rows=1, cols=len(headers)) | |
| for i, h in enumerate(headers): | |
| table.rows[0].cells[i].text = h | |
| for row in records: | |
| row_cells = table.add_row().cells | |
| for i, val in enumerate(row): | |
| row_cells[i].text = val | |
| word_path = "output.docx" | |
| doc.save(word_path) | |
| # Save PDF | |
| pdf = FPDF() | |
| pdf.add_page() | |
| pdf.set_font("Arial", size=12) | |
| for row in [headers] + records: | |
| line = ", ".join(row) | |
| pdf.cell(200, 10, txt=line, ln=True) | |
| pdf_path = "output.pdf" | |
| pdf.output(pdf_path) | |
| return excel_path, word_path, pdf_path | |
| def process_input(input_text): | |
| if not input_text.strip(): | |
| return "Please enter valid data.", None, None, None | |
| try: | |
| ai_output = generate_table_from_text(input_text) | |
| excel, word, pdf = save_files(ai_output) | |
| return ai_output, excel, word, pdf | |
| except Exception as e: | |
| return f"Error: {str(e)}", None, None, None | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 📋 AI Data Entry App\nPaste your data below. The AI will structure it and generate Excel, Word, and PDF files.") | |
| input_text = gr.Textbox(lines=10, label="Enter or paste your raw data") | |
| submit = gr.Button("Generate Files") | |
| output_text = gr.Textbox(label="AI Generated Structured Table") | |
| excel_file = gr.File(label="Download Excel File") | |
| word_file = gr.File(label="Download Word File") | |
| pdf_file = gr.File(label="Download PDF File") | |
| submit.click(fn=process_input, inputs=input_text, | |
| outputs=[output_text, excel_file, word_file, pdf_file]) | |
| demo.launch() |