Spaces:
Sleeping
Sleeping
File size: 8,973 Bytes
253afd8 |
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 |
# app.py
import streamlit as st
import pdfplumber
import pytesseract
from PIL import Image
import os
import json
import openai
import pandas as pd
import numpy as np
from io import BytesIO
from concurrent.futures import ThreadPoolExecutor
from transformers import pipeline
import hashlib
import time
# Configuration
MAX_THREADS = 4
SUPPORTED_MODELS = {
"Deepseek": "deepseek-chat",
"Llama-3-70B": "meta-llama/Meta-Llama-3-70B-Instruct",
"Mixtral": "mistralai/Mixtral-8x7B-Instruct-v0.1"
}
def secure_api_handler():
"""Advanced API key management with encryption"""
if 'api_keys' not in st.session_state:
st.session_state.api_keys = {}
with st.sidebar:
st.header("π API Management")
provider = st.selectbox("Provider", list(SUPPORTED_MODELS.keys()))
new_key = st.text_input(f"Enter {provider} API Key", type="password")
if st.button("Store Key"):
if new_key:
hashed_key = hashlib.sha256(new_key.encode()).hexdigest()
st.session_state.api_keys[provider] = hashed_key
st.success("Key stored securely")
else:
st.error("Please enter a valid API key")
def advanced_pdf_processor(uploaded_file):
"""Multi-threaded PDF processing with fault tolerance"""
st.session_state.document_data = []
def process_page(page_data):
page_num, page = page_data
try:
text = page.extract_text() or ""
images = []
for idx, img in enumerate(page.images):
try:
width = int(img["width"])
height = int(img["height"])
stream = img["stream"]
# Advanced image processing
img_mode = "RGB"
if hasattr(stream, "colorspace"):
if "/DeviceCMYK" in str(stream.colorspace):
img_mode = "CMYK"
image = Image.frombytes(img_mode, (width, height), stream.get_data())
if img_mode != "RGB":
image = image.convert("RGB")
images.append(image)
except Exception as e:
st.error(f"Image processing error: {str(e)[:100]}")
return {"page": page_num, "text": text, "images": images}
except Exception as e:
st.error(f"Page {page_num} error: {str(e)[:100]}")
return None
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
with pdfplumber.open(uploaded_file) as pdf:
results = executor.map(process_page, enumerate(pdf.pages, 1))
for result in results:
if result:
st.session_state.document_data.append(result)
st.experimental_rerun()
def hybrid_text_extractor(entry):
"""Multimodal text extraction with fallback strategies"""
text_content = entry["text"].strip()
if not text_content and entry["images"]:
ocr_texts = []
for img in entry["images"]:
try:
ocr_texts.append(pytesseract.image_to_string(img))
except Exception as e:
st.warning(f"OCR failed: {str(e)[:100]}")
text_content = " ".join(ocr_texts).strip()
return text_content
def generate_with_retry(model, messages, max_retries=3):
"""Advanced LLM generation with automatic fallback"""
for attempt in range(max_retries):
try:
client = openai.OpenAI(
base_url="https://api.deepseek.com/v1",
api_key=st.secrets.get("DEEPSEEK_API_KEY")
)
response = client.chat.completions.create(
model=SUPPORTED_MODELS[model],
messages=messages,
max_tokens=2048,
response_format={"type": "json_object"},
temperature=st.session_state.temperature
)
return json.loads(response.choices[0].message.content)
except Exception as e:
if attempt == max_retries - 1:
raise
time.sleep(2 ** attempt)
def qa_generation_workflow():
"""Enterprise-grade Q&A generation pipeline"""
if not st.session_state.document_data:
st.error("No document data loaded")
return
progress_bar = st.progress(0)
status_text = st.empty()
total_pages = len(st.session_state.document_data)
qa_pairs = []
for idx, entry in enumerate(st.session_state.document_data):
status_text.text(f"Processing page {idx+1}/{total_pages}...")
progress_bar.progress((idx+1)/total_pages)
text_content = hybrid_text_extractor(entry)
prompt = f"""Generate 3 sophisticated Q&A pairs from:
Page {entry['page']} Content:
{text_content}
Return JSON format: {{"qa_pairs": [{{"question": "...", "answer_1": "...", "answer_2": "..."}}]}}"""
try:
response = generate_with_retry(
st.session_state.model_choice,
[{"role": "user", "content": prompt}]
)
qa_pairs.extend(response.get("qa_pairs", []))
except Exception as e:
st.error(f"Generation failed: {str(e)[:100]}")
st.session_state.qa_pairs = qa_pairs
progress_bar.empty()
status_text.success("Q&A generation completed!")
def evaluation_workflow():
"""Hybrid human-AI evaluation system"""
if not st.session_state.get("qa_pairs"):
st.error("No Q&A pairs generated")
return
st.header("Quality Control Center")
with st.expander("Automated Evaluation"):
if st.button("Run AI Evaluation"):
# Implementation for automated evaluation
pass
with st.expander("Human Evaluation"):
for idx, pair in enumerate(st.session_state.qa_pairs[:5]):
st.write(f"**Question {idx+1}:** {pair['question']}")
col1, col2 = st.columns(2)
with col1:
st.write("Answer 1:", pair["answer_1"])
with col2:
st.write("Answer 2:", pair["answer_2"])
st.selectbox(
f"Select better answer for Q{idx+1}",
["Answer 1", "Answer 2", "Both Bad"],
key=f"human_eval_{idx}"
)
def main():
"""Main Streamlit application"""
st.set_page_config(
page_title="Synthetic Data Factory",
page_icon="π",
layout="wide"
)
# Initialize session state
if 'document_data' not in st.session_state:
st.session_state.document_data = []
if 'qa_pairs' not in st.session_state:
st.session_state.qa_pairs = []
# Sidebar configuration
with st.sidebar:
st.title("βοΈ Configuration")
st.session_state.model_choice = st.selectbox(
"LLM Provider",
list(SUPPORTED_MODELS.keys())
)
st.session_state.temperature = st.slider(
"Creativity Level",
0.0, 1.0, 0.3
)
st.file_uploader(
"Upload PDF Document",
type=["pdf"],
key="doc_upload"
)
# Main interface
st.title("π Synthetic Data Factory")
st.write("Enterprise-grade synthetic data generation powered by cutting-edge AI")
# Document processing pipeline
if st.session_state.doc_upload:
if st.button("Initialize Data Generation"):
with st.spinner("Deploying AI Workers..."):
advanced_pdf_processor(st.session_state.doc_upload)
# Q&A Generation
if st.session_state.document_data:
qa_generation_workflow()
# Evaluation system
if st.session_state.qa_pairs:
evaluation_workflow()
# Data export
if st.session_state.qa_pairs:
st.divider()
st.header("Data Export")
export_format = st.radio(
"Export Format",
["JSON", "CSV", "Parquet"]
)
if st.button("Generate Export Package"):
df = pd.DataFrame(st.session_state.qa_pairs)
buffer = BytesIO()
if export_format == "JSON":
df.to_json(buffer, orient="records")
elif export_format == "CSV":
df.to_csv(buffer, index=False)
else:
df.to_parquet(buffer)
st.download_button(
label="Download Dataset",
data=buffer.getvalue(),
file_name=f"synthetic_data_{int(time.time())}.{export_format.lower()}",
mime="application/octet-stream"
)
if __name__ == "__main__":
main() |