Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,11 @@ from PIL import Image
|
|
| 8 |
import io
|
| 9 |
from docx import Document
|
| 10 |
import fitz # PyMuPDF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Configure logging
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -24,6 +29,13 @@ multimodal_pipeline = pipeline("image-to-text", model="Salesforce/blip-image-cap
|
|
| 24 |
# Load a text-based model for summarization and text question answering
|
| 25 |
text_pipeline = pipeline("text2text-generation", model="t5-small")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
@app.get("/")
|
| 28 |
def read_root():
|
| 29 |
# Redirect to the static HTML file
|
|
@@ -117,6 +129,84 @@ async def visual_question_answering(file: UploadFile = File(...), question: str
|
|
| 117 |
logger.error(f"Error during visual question answering: {e}")
|
| 118 |
raise HTTPException(status_code=500, detail=str(e))
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
# Helper function to extract text from files
|
| 121 |
async def extract_text_from_file(file: UploadFile):
|
| 122 |
try:
|
|
|
|
| 8 |
import io
|
| 9 |
from docx import Document
|
| 10 |
import fitz # PyMuPDF
|
| 11 |
+
import pandas as pd
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
import seaborn as sns
|
| 14 |
+
import uuid
|
| 15 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 16 |
|
| 17 |
# Configure logging
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 29 |
# Load a text-based model for summarization and text question answering
|
| 30 |
text_pipeline = pipeline("text2text-generation", model="t5-small")
|
| 31 |
|
| 32 |
+
# Load a translation model (initialized dynamically based on target language)
|
| 33 |
+
translation_models = {
|
| 34 |
+
"fr": "Helsinki-NLP/opus-mt-en-fr",
|
| 35 |
+
"es": "Helsinki-NLP/opus-mt-en-es",
|
| 36 |
+
"de": "Helsinki-NLP/opus-mt-en-de"
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
@app.get("/")
|
| 40 |
def read_root():
|
| 41 |
# Redirect to the static HTML file
|
|
|
|
| 129 |
logger.error(f"Error during visual question answering: {e}")
|
| 130 |
raise HTTPException(status_code=500, detail=str(e))
|
| 131 |
|
| 132 |
+
@app.post("/visualize")
|
| 133 |
+
async def visualize_data(
|
| 134 |
+
file: UploadFile = File(...),
|
| 135 |
+
request: str = Form(...)
|
| 136 |
+
):
|
| 137 |
+
logger.info(f"Received Excel file for visualization: {file.filename}")
|
| 138 |
+
logger.info(f"Received visualization request: {request}")
|
| 139 |
+
|
| 140 |
+
try:
|
| 141 |
+
# Read the Excel file
|
| 142 |
+
df = pd.read_excel(io.BytesIO(await file.read()))
|
| 143 |
+
|
| 144 |
+
# Generate visualization code based on the request
|
| 145 |
+
if "bar" in request.lower():
|
| 146 |
+
code = f"""
|
| 147 |
+
import matplotlib.pyplot as plt
|
| 148 |
+
plt.bar(df['{df.columns[0]}'], df['{df.columns[1]}'])
|
| 149 |
+
plt.xlabel('{df.columns[0]}')
|
| 150 |
+
plt.ylabel('{df.columns[1]}')
|
| 151 |
+
plt.title('Bar Chart')
|
| 152 |
+
plt.show()
|
| 153 |
+
"""
|
| 154 |
+
elif "line" in request.lower():
|
| 155 |
+
code = f"""
|
| 156 |
+
import matplotlib.pyplot as plt
|
| 157 |
+
plt.plot(df['{df.columns[0]}'], df['{df.columns[1]}'])
|
| 158 |
+
plt.xlabel('{df.columns[0]}')
|
| 159 |
+
plt.ylabel('{df.columns[1]}')
|
| 160 |
+
plt.title('Line Chart')
|
| 161 |
+
plt.show()
|
| 162 |
+
"""
|
| 163 |
+
else:
|
| 164 |
+
code = f"""
|
| 165 |
+
import seaborn as sns
|
| 166 |
+
sns.pairplot(df)
|
| 167 |
+
plt.show()
|
| 168 |
+
"""
|
| 169 |
+
|
| 170 |
+
# Save the generated code to a file (optional)
|
| 171 |
+
code_filename = f"visualization_{uuid.uuid4()}.py"
|
| 172 |
+
with open(code_filename, "w") as f:
|
| 173 |
+
f.write(code)
|
| 174 |
+
|
| 175 |
+
return {"code": code, "filename": code_filename}
|
| 176 |
+
except Exception as e:
|
| 177 |
+
logger.error(f"Error during visualization code generation: {e}")
|
| 178 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 179 |
+
|
| 180 |
+
@app.post("/translate")
|
| 181 |
+
async def translate_document(
|
| 182 |
+
file: UploadFile = File(...),
|
| 183 |
+
target_language: str = Form(...)
|
| 184 |
+
):
|
| 185 |
+
logger.info(f"Received document for translation: {file.filename}")
|
| 186 |
+
logger.info(f"Target language: {target_language}")
|
| 187 |
+
|
| 188 |
+
try:
|
| 189 |
+
# Extract text from the document
|
| 190 |
+
text = await extract_text_from_file(file)
|
| 191 |
+
|
| 192 |
+
# Load a translation model based on the target language
|
| 193 |
+
if target_language in translation_models:
|
| 194 |
+
model_name = translation_models[target_language]
|
| 195 |
+
else:
|
| 196 |
+
model_name = "Helsinki-NLP/opus-mt-en-de" # Default to German
|
| 197 |
+
|
| 198 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 199 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 200 |
+
|
| 201 |
+
# Translate the text
|
| 202 |
+
translated = model.generate(**tokenizer(text, return_tensors="pt", truncation=True))
|
| 203 |
+
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 204 |
+
|
| 205 |
+
return {"translated_text": translated_text}
|
| 206 |
+
except Exception as e:
|
| 207 |
+
logger.error(f"Error during document translation: {e}")
|
| 208 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 209 |
+
|
| 210 |
# Helper function to extract text from files
|
| 211 |
async def extract_text_from_file(file: UploadFile):
|
| 212 |
try:
|