Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,209 +24,4 @@ app = FastAPI()
|
|
| 24 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 25 |
|
| 26 |
# Load a multimodal model for image captioning and visual question answering
|
| 27 |
-
|
| 28 |
-
|
| 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
|
| 42 |
-
return RedirectResponse(url="/static/index.html")
|
| 43 |
-
|
| 44 |
-
@app.post("/summarize")
|
| 45 |
-
async def summarize_text(
|
| 46 |
-
file: UploadFile = File(None), # Optional file upload
|
| 47 |
-
text: str = Form(None) # Optional manual text input
|
| 48 |
-
):
|
| 49 |
-
if file:
|
| 50 |
-
logger.info(f"Received document for summarization: {file.filename}")
|
| 51 |
-
try:
|
| 52 |
-
# Extract text from the document
|
| 53 |
-
text = await extract_text_from_file(file)
|
| 54 |
-
except Exception as e:
|
| 55 |
-
logger.error(f"Error extracting text from file: {e}")
|
| 56 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 57 |
-
elif text:
|
| 58 |
-
logger.info("Received manual text for summarization")
|
| 59 |
-
else:
|
| 60 |
-
raise HTTPException(status_code=400, detail="No file or text provided")
|
| 61 |
-
|
| 62 |
-
try:
|
| 63 |
-
# Use the text pipeline to summarize the text
|
| 64 |
-
summary = text_pipeline(f"summarize: {text}", max_length=100)
|
| 65 |
-
logger.info(f"Generated summary: {summary[0]['generated_text']}")
|
| 66 |
-
return {"summary": summary[0]['generated_text']}
|
| 67 |
-
except Exception as e:
|
| 68 |
-
logger.error(f"Error during summarization: {e}")
|
| 69 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 70 |
-
|
| 71 |
-
@app.post("/caption")
|
| 72 |
-
async def caption_image(file: UploadFile = File(...)):
|
| 73 |
-
logger.info(f"Received image for captioning: {file.filename}")
|
| 74 |
-
try:
|
| 75 |
-
# Read the image file
|
| 76 |
-
image_data = await file.read()
|
| 77 |
-
image = Image.open(io.BytesIO(image_data))
|
| 78 |
-
|
| 79 |
-
# Use the multimodal pipeline to generate a caption for the image
|
| 80 |
-
caption = multimodal_pipeline(image)
|
| 81 |
-
logger.info(f"Generated caption: {caption[0]['generated_text']}")
|
| 82 |
-
return {"caption": caption[0]['generated_text']}
|
| 83 |
-
except Exception as e:
|
| 84 |
-
logger.error(f"Error during image captioning: {e}")
|
| 85 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 86 |
-
|
| 87 |
-
@app.post("/answer")
|
| 88 |
-
async def answer_question(
|
| 89 |
-
file: UploadFile = File(None), # Optional file upload
|
| 90 |
-
text: str = Form(None), # Optional manual text input
|
| 91 |
-
question: str = Form(...) # Required question
|
| 92 |
-
):
|
| 93 |
-
if file:
|
| 94 |
-
logger.info(f"Received document for question answering: {file.filename}")
|
| 95 |
-
try:
|
| 96 |
-
# Extract text from the document
|
| 97 |
-
text = await extract_text_from_file(file)
|
| 98 |
-
except Exception as e:
|
| 99 |
-
logger.error(f"Error extracting text from file: {e}")
|
| 100 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 101 |
-
elif text:
|
| 102 |
-
logger.info("Received manual text for question answering")
|
| 103 |
-
else:
|
| 104 |
-
raise HTTPException(status_code=400, detail="No file or text provided")
|
| 105 |
-
|
| 106 |
-
try:
|
| 107 |
-
# Use the text pipeline to answer the question
|
| 108 |
-
answer = text_pipeline(f"question: {question} context: {text}")
|
| 109 |
-
logger.info(f"Generated answer: {answer[0]['generated_text']}")
|
| 110 |
-
return {"answer": answer[0]['generated_text']}
|
| 111 |
-
except Exception as e:
|
| 112 |
-
logger.error(f"Error during question answering: {e}")
|
| 113 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 114 |
-
|
| 115 |
-
@app.post("/vqa")
|
| 116 |
-
async def visual_question_answering(file: UploadFile = File(...), question: str = Form(...)):
|
| 117 |
-
logger.info(f"Received image for visual question answering: {file.filename}")
|
| 118 |
-
logger.info(f"Received question: {question}")
|
| 119 |
-
try:
|
| 120 |
-
# Read the image file
|
| 121 |
-
image_data = await file.read()
|
| 122 |
-
image = Image.open(io.BytesIO(image_data))
|
| 123 |
-
|
| 124 |
-
# Use the multimodal pipeline to answer the question about the image
|
| 125 |
-
answer = multimodal_pipeline(image, question=question)
|
| 126 |
-
logger.info(f"Generated answer: {answer[0]['generated_text']}")
|
| 127 |
-
return {"answer": answer[0]['generated_text']}
|
| 128 |
-
except Exception as e:
|
| 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:
|
| 213 |
-
if file.filename.endswith(".pdf"):
|
| 214 |
-
doc = fitz.open(stream=await file.read(), filetype="pdf")
|
| 215 |
-
text = ""
|
| 216 |
-
for page in doc:
|
| 217 |
-
text += page.get_text()
|
| 218 |
-
return text
|
| 219 |
-
elif file.filename.endswith(".docx"):
|
| 220 |
-
doc = Document(io.BytesIO(await file.read()))
|
| 221 |
-
text = "\n".join([para.text for para in doc.paragraphs])
|
| 222 |
-
return text
|
| 223 |
-
else:
|
| 224 |
-
raise ValueError("Unsupported file format. Please upload a PDF or DOCX file.")
|
| 225 |
-
except Exception as e:
|
| 226 |
-
logger.error(f"Error extracting text from file: {e}")
|
| 227 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 228 |
-
|
| 229 |
-
# Hugging Face Spaces expects the app to be served on port 7860
|
| 230 |
-
if __name__ == "__main__":
|
| 231 |
-
import uvicorn
|
| 232 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 24 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 25 |
|
| 26 |
# Load a multimodal model for image captioning and visual question answering
|
| 27 |
+
multimodal
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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