Ai1996/Data_entry321
0
1import os2import gradio as gr3import pandas as pd4from docx import Document5from fpdf import FPDF6from groq import Groq7 8# Initialize Groq client using API key from environment variable9client = Groq(api_key=os.environ.get("GROQ_API_KEY"))10 11def generate_table_from_text(text_prompt):12 response = client.chat.completions.create(13 messages=[14 {15 "role": "user",16 "content": f"Convert this into a clean table with comma-separated values:\n{text_prompt}"17 }18 ],19 model="llama3-8b-8192"20 )21 return response.choices[0].message.content22 23def save_files(data_text):24 rows = [row.strip().split(",") for row in data_text.strip().split("\n")]25 headers = rows[0]26 records = rows[1:]27 28 df = pd.DataFrame(records, columns=headers)29 30 # Save Excel31 excel_path = "output.xlsx"32 df.to_excel(excel_path, index=False)33 34 # Save Word35 doc = Document()36 doc.add_heading("Structured Data", 0)37 table = doc.add_table(rows=1, cols=len(headers))38 for i, h in enumerate(headers):39 table.rows[0].cells[i].text = h40 for row in records:41 row_cells = table.add_row().cells42 for i, val in enumerate(row):43 row_cells[i].text = val44 word_path = "output.docx"45 doc.save(word_path)46 47 # Save PDF48 pdf = FPDF()49 pdf.add_page()50 pdf.set_font("Arial", size=12)51 for row in [headers] + records:52 line = ", ".join(row)53 pdf.cell(200, 10, txt=line, ln=True)54 pdf_path = "output.pdf"55 pdf.output(pdf_path)56 57 return excel_path, word_path, pdf_path58 59def process_input(input_text):60 if not input_text.strip():61 return "Please enter valid data.", None, None, None62 63 try:64 ai_output = generate_table_from_text(input_text)65 excel, word, pdf = save_files(ai_output)66 return ai_output, excel, word, pdf67 except Exception as e:68 return f"Error: {str(e)}", None, None, None69 70with gr.Blocks() as demo:71 gr.Markdown("# ๐ AI Data Entry App\nPaste your data below. The AI will structure it and generate Excel, Word, and PDF files.")72 73 input_text = gr.Textbox(lines=10, label="Enter or paste your raw data")74 submit = gr.Button("Generate Files")75 76 output_text = gr.Textbox(label="AI Generated Structured Table")77 excel_file = gr.File(label="Download Excel File")78 word_file = gr.File(label="Download Word File")79 pdf_file = gr.File(label="Download PDF File")80 81 submit.click(fn=process_input, inputs=input_text,82 outputs=[output_text, excel_file, word_file, pdf_file])83 84demo.launch()