File size: 15,642 Bytes
6bba837
 
253afd8
 
 
6bba837
fc2842c
 
6bba837
 
fc2842c
 
 
 
 
253afd8
fc2842c
 
 
 
 
 
d6dd233
e9a68df
fc2842c
e9a68df
d6dd233
fc2842c
6bba837
fc2842c
 
 
 
6bba837
 
e9a68df
 
 
fc2842c
 
 
 
 
 
 
 
 
 
 
e9a68df
6bba837
fc2842c
6bba837
3045f18
fc2842c
e9a68df
3045f18
 
fc2842c
 
3045f18
 
fc2842c
6bba837
3045f18
 
 
e9a68df
d6dd233
3045f18
d6dd233
6bba837
fc2842c
6bba837
3045f18
fc2842c
3045f18
 
e9a68df
3045f18
d6dd233
6bba837
fc2842c
 
6bba837
fc2842c
 
6bba837
fc2842c
6bba837
fc2842c
 
 
 
 
e9a68df
fc2842c
e9a68df
 
fe72195
fc2842c
 
 
 
9f48d45
fc2842c
 
 
6bba837
fc2842c
e9a68df
58e9888
fc2842c
 
d6dd233
fc2842c
 
e9a68df
fc2842c
 
3045f18
fc2842c
3045f18
 
fc2842c
 
3045f18
fc2842c
 
3045f18
fc2842c
3045f18
fc2842c
6bba837
fc2842c
 
 
 
 
 
3045f18
e9a68df
3045f18
e9a68df
fc2842c
 
3045f18
fc2842c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58e9888
fc2842c
6bba837
fc2842c
 
6bba837
fc2842c
6bba837
fc2842c
6bba837
d6dd233
fc2842c
 
 
 
 
d6dd233
fc2842c
 
 
 
 
d6dd233
fc2842c
 
 
 
 
 
 
d6dd233
fc2842c
 
58e9888
 
fc2842c
58e9888
9f48d45
fc2842c
 
 
 
e9a68df
fc2842c
 
 
 
d6dd233
fc2842c
e9a68df
fc2842c
e9a68df
3045f18
fc2842c
 
 
 
d6dd233
 
fc2842c
d6dd233
fc2842c
 
d6dd233
e9a68df
d6dd233
 
fc2842c
d6dd233
3045f18
fc2842c
6bba837
fc2842c
6bba837
3045f18
d6dd233
fc2842c
 
 
 
 
6bba837
fc2842c
 
 
 
 
3045f18
fc2842c
 
3045f18
 
fc2842c
d6dd233
fc2842c
 
58e9888
fc2842c
 
 
6bba837
d6dd233
fc2842c
6bba837
d6dd233
6bba837
 
d6dd233
fc2842c
 
d6dd233
fc2842c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6dd233
fc2842c
 
d6dd233
fc2842c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3045f18
6bba837
fc2842c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9a68df
253afd8
6bba837
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import json
import requests
import streamlit as st
import pdfplumber
import pandas as pd
import sqlalchemy
import time
import concurrent.futures
from typing import Any, Dict, List

# Provider clients (make sure you have these installed)
try:
    from openai import OpenAI
except ImportError:
    OpenAI = None

try:
    import groq
except ImportError:
    groq = None

# Hugging Face inference URL
HF_API_URL = "https://api-inference.huggingface.co/models/"
DEFAULT_TEMPERATURE = 0.1
GROQ_MODEL = "mixtral-8x7b-32768"


class AdvancedSyntheticDataGenerator:
    """
    Advanced Synthetic Data Generator

    This class handles multiple input sources, advanced prompt engineering, and
    supports multiple LLM providers to generate synthetic data.
    """
    def __init__(self) -> None:
        self._setup_providers()
        self._setup_input_handlers()
        self._initialize_session_state()
        # A customizable prompt template (you can modify it via the UI)
        self.custom_prompt_template = (
            "You are an expert synthetic data generator. "
            "Given the data below and following the instructions provided, generate high-quality, diverse synthetic data. "
            "Ensure the output adheres to the specified format.\n\n"
            "-------------------------\n"
            "Data:\n{data}\n\n"
            "Instructions:\n{instructions}\n\n"
            "Output Format: {format}\n"
            "-------------------------\n"
        )

    def _setup_providers(self) -> None:
        """Configure available LLM providers and their initialization routines."""
        self.providers: Dict[str, Dict[str, Any]] = {
            "Deepseek": {
                "client": lambda key: OpenAI(base_url="https://api.deepseek.com/v1", api_key=key) if OpenAI else None,
                "models": ["deepseek-chat"],
            },
            "OpenAI": {
                "client": lambda key: OpenAI(api_key=key) if OpenAI else None,
                "models": ["gpt-4-turbo", "gpt-3.5-turbo"],
            },
            "Groq": {
                "client": lambda key: groq.Groq(api_key=key) if groq else None,
                "models": [GROQ_MODEL],
            },
            "HuggingFace": {
                "client": lambda key: {"headers": {"Authorization": f"Bearer {key}"}},
                "models": ["gpt2", "llama-2"],
            },
        }

    def _setup_input_handlers(self) -> None:
        """Register handlers for different input data types."""
        self.input_handlers: Dict[str, Any] = {
            "text": self.handle_text,
            "pdf": self.handle_pdf,
            "csv": self.handle_csv,
            "api": self.handle_api,
            "db": self.handle_db,
        }

    def _initialize_session_state(self) -> None:
        """Initialize Streamlit session state with default configuration."""
        defaults = {
            "config": {
                "provider": "OpenAI",
                "model": "gpt-4-turbo",
                "temperature": DEFAULT_TEMPERATURE,
                "output_format": "plain_text",  # Options: plain_text, json, csv
            },
            "api_key": "",
            "inputs": [],         # A list to store input sources
            "instructions": "",   # Custom instructions for data generation
            "synthetic_data": "", # The generated output
            "error_logs": [],     # Any errors that occur during processing
        }
        for key, value in defaults.items():
            if key not in st.session_state:
                st.session_state[key] = value

    def log_error(self, message: str) -> None:
        """Log an error message both to the session state and in the UI."""
        st.session_state.error_logs.append(message)
        st.error(message)

    # ===== INPUT HANDLERS =====
    def handle_text(self, text: str) -> Dict[str, Any]:
        return {"data": text, "source": "text"}

    def handle_pdf(self, file) -> Dict[str, Any]:
        try:
            with pdfplumber.open(file) as pdf:
                full_text = ""
                for page in pdf.pages:
                    page_text = page.extract_text() or ""
                    full_text += page_text + "\n"
                return {"data": full_text, "source": "pdf"}
        except Exception as e:
            self.log_error(f"PDF Processing Error: {e}")
            return {"data": "", "source": "pdf"}

    def handle_csv(self, file) -> Dict[str, Any]:
        try:
            df = pd.read_csv(file)
            # For simplicity, we convert the dataframe to JSON.
            return {"data": df.to_json(orient="records"), "source": "csv"}
        except Exception as e:
            self.log_error(f"CSV Processing Error: {e}")
            return {"data": "", "source": "csv"}

    def handle_api(self, config: Dict[str, str]) -> Dict[str, Any]:
        try:
            response = requests.get(config["url"], headers=config.get("headers", {}), timeout=10)
            response.raise_for_status()
            return {"data": json.dumps(response.json()), "source": "api"}
        except Exception as e:
            self.log_error(f"API Processing Error: {e}")
            return {"data": "", "source": "api"}

    def handle_db(self, config: Dict[str, str]) -> Dict[str, Any]:
        try:
            engine = sqlalchemy.create_engine(config["connection"])
            with engine.connect() as conn:
                result = conn.execute(sqlalchemy.text(config["query"]))
                rows = [dict(row) for row in result]
            return {"data": json.dumps(rows), "source": "db"}
        except Exception as e:
            self.log_error(f"Database Processing Error: {e}")
            return {"data": "", "source": "db"}

    def aggregate_inputs(self) -> str:
        """Combine all input sources into a single data string."""
        aggregated_data = ""
        for item in st.session_state.inputs:
            aggregated_data += f"Source: {item.get('source', 'unknown')}\n"
            aggregated_data += item.get("data", "") + "\n\n"
        return aggregated_data.strip()

    def build_prompt(self) -> str:
        """
        Build the complete prompt by combining the aggregated input data with
        custom instructions and the desired output format.
        """
        aggregated_data = self.aggregate_inputs()
        instructions = st.session_state.instructions or "Generate diverse, coherent synthetic data."
        output_format = st.session_state.config.get("output_format", "plain_text")
        return self.custom_prompt_template.format(
            data=aggregated_data, instructions=instructions, format=output_format
        )

    def generate_synthetic_data(self) -> bool:
        """
        Generate synthetic data by sending the built prompt to the selected LLM provider.
        Returns True if generation succeeds.
        """
        api_key = st.session_state.api_key
        if not api_key:
            self.log_error("API key is missing!")
            return False

        provider_name = st.session_state.config["provider"]
        provider_cfg = self.providers.get(provider_name)
        if not provider_cfg:
            self.log_error(f"Provider {provider_name} is not configured.")
            return False

        client_initializer = provider_cfg["client"]
        client = client_initializer(api_key)
        model = st.session_state.config["model"]
        temperature = st.session_state.config["temperature"]
        prompt = self.build_prompt()

        st.info(f"Using provider {provider_name} with model {model} at temperature {temperature:.2f}")
        # (Optionally) simulate asynchronous processing with a thread pool if needed.
        try:
            if provider_name == "HuggingFace":
                response = self._huggingface_inference(client, prompt, model)
            else:
                response = self._standard_inference(client, prompt, model, temperature)

            synthetic_data = self._parse_response(response, provider_name)
            st.session_state.synthetic_data = synthetic_data
            return True
        except Exception as e:
            self.log_error(f"Generation failed: {e}")
            return False

    def _standard_inference(self, client: Any, prompt: str, model: str, temperature: float) -> Any:
        """
        Inference method for providers using an OpenAI-compatible API.
        """
        try:
            result = client.chat.completions.create(
                model=model,
                messages=[{"role": "user", "content": prompt}],
                temperature=temperature,
            )
            return result
        except Exception as e:
            self.log_error(f"Standard Inference Error: {e}")
            return None

    def _huggingface_inference(self, client: Dict[str, Any], prompt: str, model: str) -> Any:
        """
        Inference method for the Hugging Face Inference API.
        """
        try:
            response = requests.post(
                HF_API_URL + model,
                headers=client["headers"],
                json={"inputs": prompt},
                timeout=30,
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            self.log_error(f"HuggingFace Inference Error: {e}")
            return None

    def _parse_response(self, response: Any, provider: str) -> str:
        """
        Parse the LLM response into a synthetic data string.
        """
        try:
            if provider == "HuggingFace":
                if isinstance(response, list) and "generated_text" in response[0]:
                    return response[0]["generated_text"]
                else:
                    self.log_error("Unexpected HuggingFace response format.")
                    return ""
            else:
                if response and hasattr(response, "choices") and response.choices:
                    return response.choices[0].message.content
                else:
                    self.log_error("Unexpected response format.")
                    return ""
        except Exception as e:
            self.log_error(f"Response Parsing Error: {e}")
            return ""


# ===== ADVANCED UI COMPONENTS =====

def advanced_config_ui(generator: AdvancedSyntheticDataGenerator):
    """Advanced configuration options in the sidebar."""
    with st.sidebar:
        st.header("Advanced Configuration")
        provider = st.selectbox("Select Provider", list(generator.providers.keys()))
        st.session_state.config["provider"] = provider
        provider_cfg = generator.providers[provider]

        model = st.selectbox("Select Model", provider_cfg["models"])
        st.session_state.config["model"] = model

        temperature = st.slider("Temperature", 0.0, 1.0, DEFAULT_TEMPERATURE)
        st.session_state.config["temperature"] = temperature

        output_format = st.radio("Output Format", ["plain_text", "json", "csv"])
        st.session_state.config["output_format"] = output_format

        api_key = st.text_input(f"{provider} API Key", type="password")
        st.session_state.api_key = api_key

        instructions = st.text_area("Custom Instructions",
                                    "Generate diverse, coherent synthetic data based on the input sources.",
                                    height=100)
        st.session_state.instructions = instructions

def advanced_input_ui(generator: AdvancedSyntheticDataGenerator):
    """UI for adding input sources using tabs."""
    st.header("Input Data Sources")
    tabs = st.tabs(["Text", "PDF", "CSV", "API", "Database"])
    
    with tabs[0]:
        text_input = st.text_area("Enter text input", height=150)
        if st.button("Add Text Input", key="text_input"):
            if text_input.strip():
                st.session_state.inputs.append(generator.handle_text(text_input))
                st.success("Text input added!")
    
    with tabs[1]:
        pdf_file = st.file_uploader("Upload PDF", type=["pdf"])
        if pdf_file is not None:
            st.session_state.inputs.append(generator.handle_pdf(pdf_file))
            st.success("PDF input added!")
    
    with tabs[2]:
        csv_file = st.file_uploader("Upload CSV", type=["csv"])
        if csv_file is not None:
            st.session_state.inputs.append(generator.handle_csv(csv_file))
            st.success("CSV input added!")
    
    with tabs[3]:
        api_url = st.text_input("API Endpoint URL")
        api_headers = st.text_area("API Headers (JSON format, optional)", height=100)
        if st.button("Add API Input", key="api_input"):
            headers = {}
            try:
                if api_headers:
                    headers = json.loads(api_headers)
            except Exception as e:
                generator.log_error(f"Invalid JSON for API Headers: {e}")
            st.session_state.inputs.append(generator.handle_api({"url": api_url, "headers": headers}))
            st.success("API input added!")
    
    with tabs[4]:
        db_conn = st.text_input("Database Connection String")
        db_query = st.text_area("Database Query", height=100)
        if st.button("Add Database Input", key="db_input"):
            st.session_state.inputs.append(generator.handle_db({"connection": db_conn, "query": db_query}))
            st.success("Database input added!")

def advanced_output_ui(generator: AdvancedSyntheticDataGenerator):
    """Display the generated synthetic data with various output options."""
    st.header("Synthetic Data Output")
    if st.session_state.synthetic_data:
        output_format = st.session_state.config.get("output_format", "plain_text")
        if output_format == "json":
            try:
                json_output = json.loads(st.session_state.synthetic_data)
                st.json(json_output)
            except Exception:
                st.text_area("Output", st.session_state.synthetic_data, height=300)
        else:
            st.text_area("Output", st.session_state.synthetic_data, height=300)
        st.download_button("Download Output", st.session_state.synthetic_data,
                           file_name="synthetic_data.txt", mime="text/plain")
    else:
        st.info("No synthetic data generated yet.")

def advanced_logs_ui():
    """Display error logs and debug information in an expandable section."""
    with st.expander("Error Logs & Debug Info", expanded=False):
        if st.session_state.error_logs:
            for log in st.session_state.error_logs:
                st.write(log)
        else:
            st.write("No logs yet.")


# ===== MAIN APPLICATION =====

def main() -> None:
    st.set_page_config(page_title="Advanced Synthetic Data Generator", layout="wide")
    generator = AdvancedSyntheticDataGenerator()
    advanced_config_ui(generator)
    
    # Create main tabs for Input, Output, and Logs
    main_tabs = st.tabs(["Input", "Output", "Logs"])
    
    with main_tabs[0]:
        advanced_input_ui(generator)
        if st.button("Clear Inputs"):
            st.session_state.inputs = []
            st.success("Inputs cleared!")
    
    with main_tabs[1]:
        if st.button("Generate Synthetic Data"):
            with st.spinner("Generating synthetic data..."):
                if generator.generate_synthetic_data():
                    st.success("Data generated successfully!")
                else:
                    st.error("Data generation failed. Check logs for details.")
        advanced_output_ui(generator)
    
    with main_tabs[2]:
        advanced_logs_ui()

if __name__ == "__main__":
    main()