Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
from flask import Flask, request, jsonify, render_template
|
| 4 |
from flask_cors import CORS
|
|
@@ -12,15 +13,15 @@ import json
|
|
| 12 |
import numpy as np
|
| 13 |
import logging
|
| 14 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 15 |
-
from langchain_google_genai
|
| 16 |
-
from langchain_experimental.agents import create_csv_agent
|
| 17 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 18 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 19 |
from langchain.chains.question_answering import load_qa_chain
|
| 20 |
from langchain.prompts import PromptTemplate
|
| 21 |
from langchain.docstore.document import Document
|
| 22 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 23 |
-
import plotly.express as px
|
| 24 |
import plotly
|
| 25 |
from newsapi import NewsApiClient
|
| 26 |
import certifi
|
|
@@ -45,14 +46,8 @@ newsapi.session = session
|
|
| 45 |
# Initialize the model
|
| 46 |
model = genai.GenerativeModel('gemini-pro')
|
| 47 |
|
| 48 |
-
UPLOAD_FOLDER = 'uploads'
|
| 49 |
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'docx', 'xlsx', 'csv'}
|
| 50 |
|
| 51 |
-
if not os.path.exists(UPLOAD_FOLDER):
|
| 52 |
-
os.makedirs(UPLOAD_FOLDER)
|
| 53 |
-
|
| 54 |
-
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 55 |
-
|
| 56 |
# Database setup
|
| 57 |
DATABASE = 'ai_assistant.db'
|
| 58 |
|
|
@@ -360,17 +355,20 @@ def upload_file():
|
|
| 360 |
return jsonify({'error': 'No selected file'}), 400
|
| 361 |
if file and allowed_file(file.filename):
|
| 362 |
filename = secure_filename(file.filename)
|
| 363 |
-
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 364 |
-
file.save(file_path)
|
| 365 |
|
| 366 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
logging.info(f"File uploaded successfully: {filename}")
|
| 368 |
-
extracted_text = process_document(
|
| 369 |
text_chunks = get_text_chunks(extracted_text)
|
| 370 |
analysis = analyze_document(extracted_text)
|
| 371 |
|
| 372 |
db = get_db()
|
| 373 |
-
with open(
|
| 374 |
file_data = f.read()
|
| 375 |
file_data_base64 = base64.b64encode(file_data).decode('utf-8')
|
| 376 |
cursor = db.execute('INSERT INTO files (filename, file_data, analysis) VALUES (?, ?, ?)',
|
|
@@ -380,7 +378,9 @@ def upload_file():
|
|
| 380 |
|
| 381 |
create_vector_store(text_chunks, file_id)
|
| 382 |
|
| 383 |
-
|
|
|
|
|
|
|
| 384 |
logging.info(f"File processing completed and saved to database with ID: {file_id}")
|
| 385 |
|
| 386 |
return jsonify({'file_id': file_id, 'analysis': analysis})
|
|
@@ -400,7 +400,12 @@ def plot():
|
|
| 400 |
file_data_base64 = cursor.fetchone()['file_data']
|
| 401 |
file_data = base64.b64decode(file_data_base64)
|
| 402 |
|
| 403 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
fig = px.line(df, x=df.columns[0], y=df.columns[1:])
|
| 406 |
graph_json = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
|
|
@@ -423,9 +428,9 @@ def process_csv_query():
|
|
| 423 |
file_data = base64.b64decode(file_data_base64)
|
| 424 |
|
| 425 |
# Save the CSV data to a temporary file
|
| 426 |
-
|
| 427 |
-
with open(temp_csv_path, 'wb') as temp_csv:
|
| 428 |
temp_csv.write(file_data)
|
|
|
|
| 429 |
|
| 430 |
# Create a langchain agent using the gemini-pro model
|
| 431 |
agent = create_csv_agent(GoogleGenerativeAI(model="gemini-pro"), temp_csv_path, verbose=True)
|
|
@@ -433,6 +438,9 @@ def process_csv_query():
|
|
| 433 |
# Run the query using the agent
|
| 434 |
response = agent.run(query)
|
| 435 |
|
|
|
|
|
|
|
|
|
|
| 436 |
return jsonify({'response': response})
|
| 437 |
except Exception as e:
|
| 438 |
logging.error(f'Error processing CSV query: {str(e)}', exc_info=True)
|
|
@@ -468,5 +476,4 @@ def fetch_news():
|
|
| 468 |
|
| 469 |
if __name__ == '__main__':
|
| 470 |
init_db()
|
| 471 |
-
|
| 472 |
-
app.run(host='0.0.0.0', port=port, debug=False)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import tempfile
|
| 3 |
import google.generativeai as genai
|
| 4 |
from flask import Flask, request, jsonify, render_template
|
| 5 |
from flask_cors import CORS
|
|
|
|
| 13 |
import numpy as np
|
| 14 |
import logging
|
| 15 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 16 |
+
from langchain_google_genai import GoogleGenerativeAI
|
| 17 |
+
from langchain_experimental.agents import create_csv_agent
|
| 18 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 19 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 20 |
from langchain.chains.question_answering import load_qa_chain
|
| 21 |
from langchain.prompts import PromptTemplate
|
| 22 |
from langchain.docstore.document import Document
|
| 23 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 24 |
+
import plotly.express as px
|
| 25 |
import plotly
|
| 26 |
from newsapi import NewsApiClient
|
| 27 |
import certifi
|
|
|
|
| 46 |
# Initialize the model
|
| 47 |
model = genai.GenerativeModel('gemini-pro')
|
| 48 |
|
|
|
|
| 49 |
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'docx', 'xlsx', 'csv'}
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# Database setup
|
| 52 |
DATABASE = 'ai_assistant.db'
|
| 53 |
|
|
|
|
| 355 |
return jsonify({'error': 'No selected file'}), 400
|
| 356 |
if file and allowed_file(file.filename):
|
| 357 |
filename = secure_filename(file.filename)
|
|
|
|
|
|
|
| 358 |
|
| 359 |
try:
|
| 360 |
+
# Create a temporary file
|
| 361 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
| 362 |
+
file.save(temp_file.name)
|
| 363 |
+
temp_file_path = temp_file.name
|
| 364 |
+
|
| 365 |
logging.info(f"File uploaded successfully: {filename}")
|
| 366 |
+
extracted_text = process_document(temp_file_path)
|
| 367 |
text_chunks = get_text_chunks(extracted_text)
|
| 368 |
analysis = analyze_document(extracted_text)
|
| 369 |
|
| 370 |
db = get_db()
|
| 371 |
+
with open(temp_file_path, 'rb') as f:
|
| 372 |
file_data = f.read()
|
| 373 |
file_data_base64 = base64.b64encode(file_data).decode('utf-8')
|
| 374 |
cursor = db.execute('INSERT INTO files (filename, file_data, analysis) VALUES (?, ?, ?)',
|
|
|
|
| 378 |
|
| 379 |
create_vector_store(text_chunks, file_id)
|
| 380 |
|
| 381 |
+
# Remove the temporary
|
| 382 |
+
|
| 383 |
+
os.unlink(temp_file_path)
|
| 384 |
logging.info(f"File processing completed and saved to database with ID: {file_id}")
|
| 385 |
|
| 386 |
return jsonify({'file_id': file_id, 'analysis': analysis})
|
|
|
|
| 400 |
file_data_base64 = cursor.fetchone()['file_data']
|
| 401 |
file_data = base64.b64decode(file_data_base64)
|
| 402 |
|
| 403 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx') as temp_file:
|
| 404 |
+
temp_file.write(file_data)
|
| 405 |
+
temp_file_path = temp_file.name
|
| 406 |
+
|
| 407 |
+
df = pd.read_excel(temp_file_path)
|
| 408 |
+
os.unlink(temp_file_path)
|
| 409 |
|
| 410 |
fig = px.line(df, x=df.columns[0], y=df.columns[1:])
|
| 411 |
graph_json = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
|
|
|
|
| 428 |
file_data = base64.b64decode(file_data_base64)
|
| 429 |
|
| 430 |
# Save the CSV data to a temporary file
|
| 431 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as temp_csv:
|
|
|
|
| 432 |
temp_csv.write(file_data)
|
| 433 |
+
temp_csv_path = temp_csv.name
|
| 434 |
|
| 435 |
# Create a langchain agent using the gemini-pro model
|
| 436 |
agent = create_csv_agent(GoogleGenerativeAI(model="gemini-pro"), temp_csv_path, verbose=True)
|
|
|
|
| 438 |
# Run the query using the agent
|
| 439 |
response = agent.run(query)
|
| 440 |
|
| 441 |
+
# Remove the temporary file
|
| 442 |
+
os.unlink(temp_csv_path)
|
| 443 |
+
|
| 444 |
return jsonify({'response': response})
|
| 445 |
except Exception as e:
|
| 446 |
logging.error(f'Error processing CSV query: {str(e)}', exc_info=True)
|
|
|
|
| 476 |
|
| 477 |
if __name__ == '__main__':
|
| 478 |
init_db()
|
| 479 |
+
app.run(debug=True, port=5000)
|
|
|