Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,12 +24,12 @@ import urllib.parse
|
|
| 24 |
import re
|
| 25 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 26 |
from langchain_community.document_loaders import PyPDFLoader, UnstructuredCSVLoader, UnstructuredExcelLoader, Docx2txtLoader, UnstructuredPowerPointLoader
|
| 27 |
-
from langchain.chains import StuffDocumentsChain
|
| 28 |
from langchain.chains.llm import LLMChain
|
| 29 |
from langchain.prompts import PromptTemplate
|
| 30 |
from langchain.vectorstores import FAISS
|
| 31 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 32 |
from langchain.text_splitter import CharacterTextSplitter
|
|
|
|
| 33 |
|
| 34 |
app = FastAPI()
|
| 35 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
@@ -177,11 +177,12 @@ def generate_plot(df, plot_path, plot_type):
|
|
| 177 |
plt.close(fig)
|
| 178 |
return plot_path
|
| 179 |
|
|
|
|
| 180 |
@app.get("/", response_class=HTMLResponse)
|
| 181 |
async def read_form(request: Request):
|
| 182 |
return templates.TemplateResponse("upload.html", {"request": request})
|
| 183 |
|
| 184 |
-
@app.post("/process", response_class=HTMLResponse)
|
| 185 |
async def process_file(request: Request, file: UploadFile = File(...)):
|
| 186 |
global df, uploaded_file, document_analyzed, file_path, file_extension
|
| 187 |
uploaded_file = file
|
|
@@ -213,6 +214,7 @@ async def process_file(request: Request, file: UploadFile = File(...)):
|
|
| 213 |
@app.post("/result")
|
| 214 |
async def result(request: Request,
|
| 215 |
target: str = Form(...),
|
|
|
|
| 216 |
algorithm: str = Form(...)):
|
| 217 |
global df, api
|
| 218 |
global plot1_path, plot2_path, plot3_path, plot4_path, plot5_path, plot6_path, plot7_path, plot8_path, plot9_path, plot10_path, plot11_path
|
|
@@ -221,6 +223,14 @@ async def result(request: Request,
|
|
| 221 |
|
| 222 |
api = "AIzaSyD3drCF1KnfAfTNYGNIBJNS_nAry7kzlxg"
|
| 223 |
excluded_words = ["name", "postal", "date", "phone", "address", "id"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
if df[target].dtype in ['float64', 'int64']:
|
| 226 |
unique_values = df[target].nunique()
|
|
@@ -296,21 +306,7 @@ async def result(request: Request,
|
|
| 296 |
plt.savefig(plot3_path)
|
| 297 |
plt.close(fig)
|
| 298 |
|
| 299 |
-
|
| 300 |
-
def to_markdown(text):
|
| 301 |
-
text = text.replace('•', ' *')
|
| 302 |
-
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
|
| 303 |
-
|
| 304 |
-
genai.configure(api_key=api)
|
| 305 |
-
|
| 306 |
-
import PIL.Image
|
| 307 |
-
|
| 308 |
-
img = PIL.Image.open("static/multiclass_barplot.png")
|
| 309 |
-
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 310 |
-
#response = model.generate_content(img)
|
| 311 |
-
response = model.generate_content(["As a marketing consulant, I want to understand consumer insighst based on the chart and the market context so I can use the key findings to formulate actionable insights", img])
|
| 312 |
-
response.resolve()
|
| 313 |
-
response3 = format_text(response.text)
|
| 314 |
|
| 315 |
|
| 316 |
if method == "Classification":
|
|
@@ -320,22 +316,7 @@ async def result(request: Request,
|
|
| 320 |
pairplot_fig.savefig(plot6_path) # Save the pairplot as a PNG file
|
| 321 |
|
| 322 |
|
| 323 |
-
|
| 324 |
-
genai.configure(api_key=api)
|
| 325 |
-
img = PIL.Image.open(plot6_path)
|
| 326 |
-
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 327 |
-
|
| 328 |
-
# Generate response based on the pairplot
|
| 329 |
-
response = model.generate_content([
|
| 330 |
-
"You are a professional Data Analyst, write the complete conclusion and actionable insight based on the image. Explain it by points.",
|
| 331 |
-
img
|
| 332 |
-
])
|
| 333 |
-
response.resolve()
|
| 334 |
-
|
| 335 |
-
# Assign the response to response6
|
| 336 |
-
response6 = format_text(response.text)
|
| 337 |
-
|
| 338 |
-
# Include response6 and plot6_path in the data dictionary to be passed to the template
|
| 339 |
|
| 340 |
|
| 341 |
if method == "Classification":
|
|
@@ -357,7 +338,7 @@ async def result(request: Request,
|
|
| 357 |
for i, var in enumerate(int_vars):
|
| 358 |
top_categories = df[var].value_counts().nlargest(10).index
|
| 359 |
filtered_df = df[df[var].notnull() & df[var].isin(top_categories)]
|
| 360 |
-
sns.histplot(data=df, x=var, hue=target, kde=
|
| 361 |
axs[i].set_title(var)
|
| 362 |
|
| 363 |
# Annotate the subplot with sample size
|
|
@@ -376,58 +357,20 @@ async def result(request: Request,
|
|
| 376 |
plt.savefig(plot4_path)
|
| 377 |
plt.close(fig)
|
| 378 |
|
| 379 |
-
|
| 380 |
-
def to_markdown(text):
|
| 381 |
-
text = text.replace('•', ' *')
|
| 382 |
-
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
|
| 383 |
-
|
| 384 |
-
genai.configure(api_key=api)
|
| 385 |
-
|
| 386 |
-
import PIL.Image
|
| 387 |
-
|
| 388 |
-
img = PIL.Image.open("static/multiclass_histplot.png")
|
| 389 |
-
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 390 |
-
response4 = model.generate_content(img)
|
| 391 |
-
response4 = model.generate_content(["As a marketing consulant, I want to understand consumer insighst based on the chart and the market context so I can use the key findings to formulate actionable insights", img])
|
| 392 |
-
response4.resolve()
|
| 393 |
-
response4 = format_text(response4.text)
|
| 394 |
-
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
|
|
|
| 399 |
# Generate Pairplot
|
| 400 |
pairplot_fig = sns.pairplot(df)
|
| 401 |
plot5_path = "static/pair2.png"
|
| 402 |
pairplot_fig.savefig(plot5_path) # Save the pairplot as a PNG file
|
| 403 |
|
| 404 |
-
|
| 405 |
-
genai.configure(api_key=api)
|
| 406 |
-
img = Image.open(plot5_path)
|
| 407 |
-
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 408 |
-
|
| 409 |
-
# Generate response based on the pairplot
|
| 410 |
-
response = model.generate_content([
|
| 411 |
-
"You are a professional Data Analyst, write the complete conclusion and actionable insight based on the image. Explain it by points.",
|
| 412 |
-
img
|
| 413 |
-
])
|
| 414 |
-
response.resolve()
|
| 415 |
-
|
| 416 |
-
# Assign the response to response5
|
| 417 |
-
response5 = format_text(response.text)
|
| 418 |
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
genai.configure(api_key=api)
|
| 423 |
-
img = Image.open(plot_path)
|
| 424 |
-
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 425 |
-
response = model.generate_content([
|
| 426 |
-
" As a marketing consultant, I want to understand consumer insights based on the chart and the market context so I can use the key findings to formulate actionable insights",
|
| 427 |
-
img
|
| 428 |
-
])
|
| 429 |
-
response.resolve()
|
| 430 |
-
return response.text
|
| 431 |
|
| 432 |
plot1_path = generate_plot(df, 'static/plot1.png', 'countplot')
|
| 433 |
plot2_path = generate_plot(df, 'static/plot2.png', 'histplot')
|
|
@@ -456,7 +399,7 @@ async def result(request: Request,
|
|
| 456 |
plt.savefig(plot7_path)
|
| 457 |
plt.close(fig)
|
| 458 |
|
| 459 |
-
img = Image.open(plot7_path)
|
| 460 |
response7 = format_text((generate_gemini_response(plot7_path)))
|
| 461 |
|
| 462 |
|
|
@@ -526,7 +469,7 @@ async def result(request: Request,
|
|
| 526 |
ax.set_title('Top 10 Feature Importance (Decision Tree Regressor)', fontsize=18)
|
| 527 |
plot8_path = "static/dtree_regressor.png"
|
| 528 |
plt.savefig(plot8_path)
|
| 529 |
-
img = Image.open(plot8_path)
|
| 530 |
response8 = format_text((generate_gemini_response(plot8_path)))
|
| 531 |
|
| 532 |
|
|
@@ -561,7 +504,7 @@ async def result(request: Request,
|
|
| 561 |
ax.set_title('Top 10 Feature Importance (Decision Tree Classifier)', fontsize=18)
|
| 562 |
plot9_path = "static/dtree_classifier.png"
|
| 563 |
plt.savefig(plot9_path)
|
| 564 |
-
img = Image.open(plot9_path)
|
| 565 |
response9 = format_text((generate_gemini_response(plot9_path)))
|
| 566 |
|
| 567 |
|
|
@@ -601,7 +544,7 @@ async def result(request: Request,
|
|
| 601 |
ax.set_title('Top 10 Feature Importance (Random Forest Regressor)', fontsize=18)
|
| 602 |
plot10_path = "static/rf_regressor.png"
|
| 603 |
plt.savefig(plot10_path)
|
| 604 |
-
img = Image.open(plot10_path)
|
| 605 |
response10 = format_text((generate_gemini_response(plot10_path)))
|
| 606 |
|
| 607 |
elif method == "Classification":
|
|
@@ -633,7 +576,7 @@ async def result(request: Request,
|
|
| 633 |
ax.set_title('Top 10 Feature Importance (Random Forest Classifier)', fontsize=18)
|
| 634 |
plot11_path = "static/rf_classifier.png"
|
| 635 |
plt.savefig(plot11_path)
|
| 636 |
-
img = Image.open(plot11_path)
|
| 637 |
response11 = format_text((generate_gemini_response(plot11_path)))
|
| 638 |
|
| 639 |
|
|
@@ -704,7 +647,7 @@ async def ask_question(request: Request, question: str = Form(...)):
|
|
| 704 |
raise HTTPException(status_code=400, detail="No file has been uploaded yet.")
|
| 705 |
|
| 706 |
# Initialize the LLM model
|
| 707 |
-
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash
|
| 708 |
|
| 709 |
# Determine the file extension and select the appropriate loader
|
| 710 |
file_path = ''
|
|
@@ -850,4 +793,6 @@ def save_to_json(question_responses):
|
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
-
|
|
|
|
|
|
|
|
|
| 24 |
import re
|
| 25 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 26 |
from langchain_community.document_loaders import PyPDFLoader, UnstructuredCSVLoader, UnstructuredExcelLoader, Docx2txtLoader, UnstructuredPowerPointLoader
|
|
|
|
| 27 |
from langchain.chains.llm import LLMChain
|
| 28 |
from langchain.prompts import PromptTemplate
|
| 29 |
from langchain.vectorstores import FAISS
|
| 30 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 31 |
from langchain.text_splitter import CharacterTextSplitter
|
| 32 |
+
import PIL.Image
|
| 33 |
|
| 34 |
app = FastAPI()
|
| 35 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
| 177 |
plt.close(fig)
|
| 178 |
return plot_path
|
| 179 |
|
| 180 |
+
|
| 181 |
@app.get("/", response_class=HTMLResponse)
|
| 182 |
async def read_form(request: Request):
|
| 183 |
return templates.TemplateResponse("upload.html", {"request": request})
|
| 184 |
|
| 185 |
+
@app.post("/process/", response_class=HTMLResponse)
|
| 186 |
async def process_file(request: Request, file: UploadFile = File(...)):
|
| 187 |
global df, uploaded_file, document_analyzed, file_path, file_extension
|
| 188 |
uploaded_file = file
|
|
|
|
| 214 |
@app.post("/result")
|
| 215 |
async def result(request: Request,
|
| 216 |
target: str = Form(...),
|
| 217 |
+
question: str = Form(...),
|
| 218 |
algorithm: str = Form(...)):
|
| 219 |
global df, api
|
| 220 |
global plot1_path, plot2_path, plot3_path, plot4_path, plot5_path, plot6_path, plot7_path, plot8_path, plot9_path, plot10_path, plot11_path
|
|
|
|
| 223 |
|
| 224 |
api = "AIzaSyD3drCF1KnfAfTNYGNIBJNS_nAry7kzlxg"
|
| 225 |
excluded_words = ["name", "postal", "date", "phone", "address", "id"]
|
| 226 |
+
|
| 227 |
+
def generate_gemini_response(plot_path):
|
| 228 |
+
genai.configure(api_key=api)
|
| 229 |
+
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 230 |
+
img = Image.open(plot_path)
|
| 231 |
+
response = model.generate_content([question + " As a marketing consultant, I want to understand consumer insights based on the chart and the market context so I can use the key findings to formulate actionable insights.", img])
|
| 232 |
+
response.resolve()
|
| 233 |
+
return response.text
|
| 234 |
|
| 235 |
if df[target].dtype in ['float64', 'int64']:
|
| 236 |
unique_values = df[target].nunique()
|
|
|
|
| 306 |
plt.savefig(plot3_path)
|
| 307 |
plt.close(fig)
|
| 308 |
|
| 309 |
+
response3 = format_text(generate_gemini_response(plot3_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
|
| 311 |
|
| 312 |
if method == "Classification":
|
|
|
|
| 316 |
pairplot_fig.savefig(plot6_path) # Save the pairplot as a PNG file
|
| 317 |
|
| 318 |
|
| 319 |
+
response6 = format_text(generate_gemini_response(plot6_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
|
| 322 |
if method == "Classification":
|
|
|
|
| 338 |
for i, var in enumerate(int_vars):
|
| 339 |
top_categories = df[var].value_counts().nlargest(10).index
|
| 340 |
filtered_df = df[df[var].notnull() & df[var].isin(top_categories)]
|
| 341 |
+
sns.histplot(data=df, x=var, hue=target, kde=True, ax=axs[i], stat="percent")
|
| 342 |
axs[i].set_title(var)
|
| 343 |
|
| 344 |
# Annotate the subplot with sample size
|
|
|
|
| 357 |
plt.savefig(plot4_path)
|
| 358 |
plt.close(fig)
|
| 359 |
|
| 360 |
+
response4 = format_text(generate_gemini_response(plot4_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
+
import PIL.Image
|
| 366 |
# Generate Pairplot
|
| 367 |
pairplot_fig = sns.pairplot(df)
|
| 368 |
plot5_path = "static/pair2.png"
|
| 369 |
pairplot_fig.savefig(plot5_path) # Save the pairplot as a PNG file
|
| 370 |
|
| 371 |
+
response5 = format_text(generate_gemini_response(plot5_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
|
| 373 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
|
| 375 |
plot1_path = generate_plot(df, 'static/plot1.png', 'countplot')
|
| 376 |
plot2_path = generate_plot(df, 'static/plot2.png', 'histplot')
|
|
|
|
| 399 |
plt.savefig(plot7_path)
|
| 400 |
plt.close(fig)
|
| 401 |
|
| 402 |
+
img = PIL.Image.open(plot7_path)
|
| 403 |
response7 = format_text((generate_gemini_response(plot7_path)))
|
| 404 |
|
| 405 |
|
|
|
|
| 469 |
ax.set_title('Top 10 Feature Importance (Decision Tree Regressor)', fontsize=18)
|
| 470 |
plot8_path = "static/dtree_regressor.png"
|
| 471 |
plt.savefig(plot8_path)
|
| 472 |
+
img = PIL.Image.open(plot8_path)
|
| 473 |
response8 = format_text((generate_gemini_response(plot8_path)))
|
| 474 |
|
| 475 |
|
|
|
|
| 504 |
ax.set_title('Top 10 Feature Importance (Decision Tree Classifier)', fontsize=18)
|
| 505 |
plot9_path = "static/dtree_classifier.png"
|
| 506 |
plt.savefig(plot9_path)
|
| 507 |
+
img = PIL.Image.open(plot9_path)
|
| 508 |
response9 = format_text((generate_gemini_response(plot9_path)))
|
| 509 |
|
| 510 |
|
|
|
|
| 544 |
ax.set_title('Top 10 Feature Importance (Random Forest Regressor)', fontsize=18)
|
| 545 |
plot10_path = "static/rf_regressor.png"
|
| 546 |
plt.savefig(plot10_path)
|
| 547 |
+
img = PIL.Image.open(plot10_path)
|
| 548 |
response10 = format_text((generate_gemini_response(plot10_path)))
|
| 549 |
|
| 550 |
elif method == "Classification":
|
|
|
|
| 576 |
ax.set_title('Top 10 Feature Importance (Random Forest Classifier)', fontsize=18)
|
| 577 |
plot11_path = "static/rf_classifier.png"
|
| 578 |
plt.savefig(plot11_path)
|
| 579 |
+
img = PIL.Image.open(plot11_path)
|
| 580 |
response11 = format_text((generate_gemini_response(plot11_path)))
|
| 581 |
|
| 582 |
|
|
|
|
| 647 |
raise HTTPException(status_code=400, detail="No file has been uploaded yet.")
|
| 648 |
|
| 649 |
# Initialize the LLM model
|
| 650 |
+
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", google_api_key=api)
|
| 651 |
|
| 652 |
# Determine the file extension and select the appropriate loader
|
| 653 |
file_path = ''
|
|
|
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
+
if __name__ == "__main__":
|
| 797 |
+
import uvicorn
|
| 798 |
+
uvicorn.run(app, host="127.0.0.1", port=8000)
|