Spaces:
Sleeping

File size: 2,605 Bytes
9c2c784
 
 
 
 
80a5663
9c2c784
80a5663
 
9c2c784
80a5663
9c2c784
80a5663
 
 
 
 
 
9c2c784
 
80a5663
9c2c784
80a5663
 
9c2c784
80a5663
9c2c784
80a5663
 
 
9c2c784
 
 
80a5663
9c2c784
80a5663
9c2c784
 
 
80a5663
 
9c2c784
80a5663
9c2c784
 
 
80a5663
9c2c784
 
 
80a5663
 
 
9c2c784
 
 
 
 
80a5663
 
 
 
 
 
 
 
 
 
9c2c784
 
80a5663
9c2c784
80a5663
9c2c784
 
80a5663
 
 
 
9c2c784
80a5663
 
9c2c784
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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()