Spaces:
Sleeping
Sleeping
Create app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import osimport gradio as grimport pandas as pdfrom groq import Groqfrom docx import Documentfrom fpdf import FPDF
# Set your Groq API KeyGROQ_API_KEY = os.getenv("GROQ_API_KEY") or "your_groq_api_key_here"client = Groq(api_key=GROQ_API_KEY)
# Step 1: Use LLaMA3 to structure the input datadef generate_structured_data(user_input): prompt = ( f"Format the following text into a clean, structured table with comma-separated rows.\n" f"Only include headers and rows, no extra explanation.\n\n{user_input}" ) response = client.chat.completions.create( messages=[{"role": "user", "content": prompt}], model="llama3-8b-8192" ) return response.choices[0].message.content.strip()
# Step 2: Save to Excel, Word, and PDFdef save_to_files(structured_text): # Process text into rows rows = [row.strip().split(",") for row in structured_text.split("\n") if row.strip()] headers = rows[0] data = rows[1:]
# Excel df = pd.DataFrame(data, columns=headers) excel_path = "output.xlsx" df.to_excel(excel_path, index=False)
# Word doc = Document() doc.add_heading("Structured Data", level=1) 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 data: cells = table.add_row().cells for i, val in enumerate(row): cells[i].text = val word_path = "output.docx" doc.save(word_path)
# PDF pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) for row in [headers] + data: pdf.cell(200, 10, txt=", ".join(row), ln=True) pdf_path = "output.pdf" pdf.output(pdf_path)
return excel_path, word_path, pdf_path
# Step 3: Gradio UIdef handle_input(user_input): structured = generate_structured_data(user_input) excel, word, pdf = save_to_files(structured) return structured, excel, word, pdf
with gr.Blocks() as demo: gr.Markdown("# 🧠 Data Entry AI App\nUpload raw text — get Excel, Word, and PDF!")
user_input = gr.Textbox(lines=10, label="Enter or paste raw data") submit = gr.Button("Generate Files")
output_text = gr.Textbox(label="Structured Data (AI Output)") excel_out = gr.File(label="Download Excel (.xlsx)") word_out = gr.File(label="Download Word (.docx)") pdf_out = gr.File(label="Download PDF (.pdf)")
submit.click(fn=handle_input, inputs=user_input, outputs=[output_text, excel_out, word_out, pdf_out])
demo.launch()app.pyAdded+80