Spaces:
Sleeping
Sleeping
File size: 5,880 Bytes
4603e82 1a0d7ed 4603e82 5f2f34f 4603e82 06154c7 4603e82 f8ef4e3 4603e82 44041d6 988c7cc 4603e82 988c7cc 4603e82 988c7cc 12ad28b f8ef4e3 d537a56 4437adf 4603e82 12ad28b 4603e82 06154c7 4603e82 06154c7 4603e82 988c7cc 4603e82 |
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 |
from flask import Flask,request,make_response
import os
import logging
from dotenv import load_dotenv
from heyoo import WhatsApp
import random
import shutil
from tempfile import NamedTemporaryFile
import assemblyai as aai
from pandasai.llm import GoogleGemini
from pandasai import SmartDataframe
from pandasai import Agent
from pandasai.responses.response_parser import ResponseParser
from langchain_experimental.agents import create_pandas_dataframe_agent
import pandas as pd
from langchain_google_genai import GoogleGenerativeAI
from langchain_google_genai.chat_models import ChatGoogleGenerativeAI
import requests
import base64
from pandasai.helpers import path
import uuid
import pandasai as pai
# load env data
load_dotenv()
'''
def modify_file_path(file_path):
guid = uuid.uuid4()
print(guid)
guid = str(uuid.uuid4())
new_filename = f"{guid}temp_chart.png"
new_file_path = os.path.join("/home/runner/Flasktestrairo/plots/", new_filename)
# Copy the file
shutil.copy2(file_path, new_file_path)
return new_file_path
'''
guid = uuid.uuid4()
new_filename = f"{guid}"
user_defined_path = os.path.join("/plots/", new_filename)
class FlaskResponse(ResponseParser):
def __init__(self, context) -> None:
super().__init__(context)
def format_dataframe(self, result):
return result['value'].to_html()
def format_plot(self, result):
# Save the plot using savefig
try:
img_path = result['value']
except ValueError:
img_path = str(result['value'])
print("value error!", img_path)
print("response_class_path:", img_path)
return img_path
def format_other(self, result):
return str(result['value'])
# messenger object
messenger = WhatsApp(
os.environ["whatsapp_token"],
phone_number_id=os.environ["phone_number_id"] )
aai.settings.api_key = os.environ["aai_key"]
transcriber = aai.Transcriber()
g_api_key = os.environ["google_api_key"]
app = Flask(__name__)
VERIFY_TOKEN = "30cca545-3838-48b2-80a7-9e43b1ae8ce4"
#df = pd.read_excel("craig.xlsx")
df = pd.read_csv("small_business_data2.csv")
llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=g_api_key, temperature=0.2)
def generateResponse(prompt):
llm = GoogleGemini(api_key=g_api_key)
pandas_agent = Agent(df,config={"llm":llm, "save_charts_path": user_defined_path,"save_charts": True,"response_parser":FlaskResponse, "enable_cache": False, "conversational":True})
answer = pandas_agent.chat(prompt)
return answer
img_ID = "344744a88ad1098"
img_secret="3c542a40c215327045d7155bddfd8b8bc84aebbf"
url = "https://api.imgur.com/3/image"
headers = {"Authorization": f"Client-ID {img_ID}"}
def respond(query_str:str):
response = "hello, I don't have a brain yet"
return response
@app.route("/", methods=["GET", "POST"])
def hook():
if request.method == "GET":
if request.args.get("hub.verify_token") == VERIFY_TOKEN:
logging.info("Verified webhook")
response = make_response(request.args.get("hub.challenge"), 200)
response.mimetype = "text/plain"
return response
logging.error("Webhook Verification failed")
return "Invalid verification token"
# get message update..
data = request.get_json()
changed_field = messenger.changed_field(data)
if changed_field == "messages":
new_message = messenger.get_mobile(data)
if new_message:
mobile = messenger.get_mobile(data)
message_type = messenger.get_message_type(data)
if message_type == "text":
message = messenger.get_message(data)
# Handle greetings
if message.lower() in ("hi", "hello", "help", "how are you"):
response = "Hi there! My name is BuzyHelper. How can I help you today?"
messenger.send_message(message=f"{response}",recipient_id=mobile)
else:
response = str(generateResponse(message))
print("Response:", response)
logging.info(f"\nAnswer: {response}\n")
# Check if response is a string and represents a valid image path
if isinstance(response, str) and os.path.isfile(os.path.join(response)):
image_path = os.path.join(response)
print("My image path:", image_path)
with open(image_path, "rb") as file:
data = file.read()
base64_data = base64.b64encode(data)
# Upload image to Imgur and get URL
response = requests.post(url, headers=headers, data={"image": base64_data})
url1= response.json()["data"]["link"]
print(url1)
messenger.send_image(image=url1, recipient_id=mobile)
else:
# Handle cases where response is not a valid image path
messenger.send_message(message=f"{response}", recipient_id=mobile)
elif message_type == "audio":
audio = messenger.get_audio(data)
audio_id, mime_type = audio["id"], audio["mime_type"]
audio_url = messenger.query_media_url(audio_id)
audio_filename = messenger.download_media(audio_url, mime_type)
transcript =transcriber.transcribe(audio_filename)
print(audio_filename)
print(transcript.text)
res = transcript.text
logging.info(f"\nAudio: {audio}\n")
response = str(generateResponse(res))
if isinstance(response, str):
messenger.send_message(message=f"{response}", recipient_id=mobile)
elif isinstance(response, str) and os.path.isfile(response):
messenger.send_image(image_path=response, recipient_id=mobile)
else:
messenger.send_message(message="Please send me text or audio messages",recipient_id=mobile)
return "ok"
if __name__ == '__main__':
app.run(debug=True,host="0.0.0.0", port=7860) |