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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# coding: utf-8
|
| 3 |
+
|
| 4 |
+
# ## Recopila información
|
| 5 |
+
|
| 6 |
+
# In[132]:
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import csv
|
| 11 |
+
import smtplib
|
| 12 |
+
from email.message import EmailMessage
|
| 13 |
+
|
| 14 |
+
# Function to handle the form submission
|
| 15 |
+
def handle_form(product, country, business_data, your_name, your_mail, business_name):
|
| 16 |
+
# Save data to a CSV file
|
| 17 |
+
with open('data.csv', 'a', newline='') as csvfile:
|
| 18 |
+
writer = csv.writer(csvfile)
|
| 19 |
+
|
| 20 |
+
# Check if the CSV file is empty (no header), then write the header
|
| 21 |
+
if csvfile.tell() == 0:
|
| 22 |
+
writer.writerow(["Product", "Country", "Business Data", "Your Name", "Your Mail", "Business Name"])
|
| 23 |
+
|
| 24 |
+
writer.writerow([product, country, business_data, your_name, your_mail, business_name])
|
| 25 |
+
|
| 26 |
+
# Send an email
|
| 27 |
+
send_email(your_mail)
|
| 28 |
+
|
| 29 |
+
return "Thanks"
|
| 30 |
+
|
| 31 |
+
# Function to send an email
|
| 32 |
+
def send_email(email_address):
|
| 33 |
+
msg = EmailMessage()
|
| 34 |
+
msg.set_content("Thank you for your submission!")
|
| 35 |
+
msg['Subject'] = 'Form Submission'
|
| 36 |
+
msg['From'] = 'your_email@gmail.com' # Replace with your email
|
| 37 |
+
msg['To'] = email_address
|
| 38 |
+
|
| 39 |
+
with smtplib.SMTP('smtp.gmail.com', 587) as server:
|
| 40 |
+
server.starttls()
|
| 41 |
+
server.login('your_email@gmail.com', 'your_password') # Replace with your email and password
|
| 42 |
+
server.send_message(msg)
|
| 43 |
+
|
| 44 |
+
# Create a Gradio interface
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=handle_form,
|
| 47 |
+
inputs=[
|
| 48 |
+
gr.inputs.Textbox(label="Product"),
|
| 49 |
+
gr.inputs.Textbox(label="Country"),
|
| 50 |
+
gr.inputs.Textbox(label="Business data"),
|
| 51 |
+
gr.inputs.Textbox(label="Your Name"),
|
| 52 |
+
gr.inputs.Textbox(label="Your Mail"),
|
| 53 |
+
gr.inputs.Textbox(label="Business Name")
|
| 54 |
+
],
|
| 55 |
+
outputs=gr.outputs.Textbox()
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Launch the interface
|
| 59 |
+
iface.launch()
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# ## Genera la consulta a BARD
|
| 63 |
+
|
| 64 |
+
# In[133]:
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
from bardapi import Bard
|
| 68 |
+
import os
|
| 69 |
+
import pandas as pd
|
| 70 |
+
|
| 71 |
+
# Set the BARD API key
|
| 72 |
+
os.environ['_BARD_API_KEY'] = "ZgiPqEGAj8g2ly1fEp-RIbegbHycIGqpxB0Z43awd5bZfqOdKB_LYcvLeh6KdOGuyJRSIg."
|
| 73 |
+
|
| 74 |
+
def generate_prompt(field1, field2):
|
| 75 |
+
return f"what norms I need to export '{field1}' to '{field2}' from Colombia?"
|
| 76 |
+
|
| 77 |
+
# Read data from CSV file into a DataFrame
|
| 78 |
+
data_file = "/Users/manuelbeltran/Documents/EMPRESAS/SAPIENS/SpinOffs/2023-ai001/data.csv"
|
| 79 |
+
df = pd.read_csv(data_file)
|
| 80 |
+
|
| 81 |
+
# Extract values from DataFrame
|
| 82 |
+
field1 = df["Product"].iloc[0] # Assuming you want the first value
|
| 83 |
+
field2 = df["Country"].iloc[0] # Assuming you want the first value
|
| 84 |
+
|
| 85 |
+
# Generate the prompt
|
| 86 |
+
prompt = generate_prompt(field1, field2)
|
| 87 |
+
|
| 88 |
+
# Use the Bard API to get an answer
|
| 89 |
+
answer = Bard().get_answer(prompt)['content']
|
| 90 |
+
print(answer)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# In[134]:
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
answer
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
# In[135]:
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
import gradio as gr
|
| 103 |
+
|
| 104 |
+
# Define a function to get the value of an existing variable
|
| 105 |
+
def get_variable_value():
|
| 106 |
+
# Replace this with your logic to retrieve the variable value
|
| 107 |
+
my_variable = answer
|
| 108 |
+
return f"The value of the variable is: {my_variable}"
|
| 109 |
+
|
| 110 |
+
# Create the Gradio interface
|
| 111 |
+
iface = gr.Interface(
|
| 112 |
+
fn=get_variable_value,
|
| 113 |
+
inputs=None, # No input needed
|
| 114 |
+
outputs="text",
|
| 115 |
+
layout="vertical",
|
| 116 |
+
title="Variable Value Displayer",
|
| 117 |
+
description="This interface displays the value of an existing variable."
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Launch the interface
|
| 121 |
+
iface.launch()
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# In[136]:
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
sender_email = "maniambuj@gmail.com"
|
| 128 |
+
sender_password = "imvqqabnoicocfhl"
|
| 129 |
+
recipient_email = df.iloc[0,4]
|
| 130 |
+
message = answer
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
# In[137]:
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
import smtplib
|
| 137 |
+
try:
|
| 138 |
+
server = smtplib.SMTP("smtp.gmail.com", 587)
|
| 139 |
+
server.starttls()
|
| 140 |
+
server.login('miambeltranuj@gmail.com','imvqqabnoicocfhl')
|
| 141 |
+
server.sendmail(sender_email, recipient_email, message)
|
| 142 |
+
print("Email sent successfully.")
|
| 143 |
+
except Exception as e:
|
| 144 |
+
print("Error sending email:", str(e))
|
| 145 |
+
finally:
|
| 146 |
+
server.quit()
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# In[144]:
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
df['answer'] = answer
|
| 153 |
+
df['answer']
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
# In[145]:
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
df.dtypes
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
# In[147]:
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
df.to_csv('data.csv', index=False)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
# In[148]:
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
import psycopg2
|
| 172 |
+
from psycopg2 import sql
|
| 173 |
+
import csv
|
| 174 |
+
|
| 175 |
+
# Database connection parameters
|
| 176 |
+
dbname = "mania"
|
| 177 |
+
user = "postgres"
|
| 178 |
+
password = "Juana2016$"
|
| 179 |
+
host = "localhost" # Update this with the correct host or IP address
|
| 180 |
+
port = "5433"
|
| 181 |
+
|
| 182 |
+
# Connect to the PostgreSQL database
|
| 183 |
+
conn = psycopg2.connect(dbname=dbname, user=user, password=password, host=host, port=port)
|
| 184 |
+
cur = conn.cursor()
|
| 185 |
+
|
| 186 |
+
# Table and CSV file information
|
| 187 |
+
table_name = "inorma"
|
| 188 |
+
csv_file_path = "/Users/manuelbeltran/Documents/EMPRESAS/SAPIENS/SpinOffs/2023-ai001/data.csv"
|
| 189 |
+
|
| 190 |
+
# Create a list of column names
|
| 191 |
+
columns = ["product", "country", "businessdata", "yourname", "yourmail", "businessname","answer"]
|
| 192 |
+
|
| 193 |
+
# Build the SQL query for copying data from the CSV file
|
| 194 |
+
copy_sql = sql.SQL("COPY {} ({}) FROM STDIN WITH CSV HEADER").format(
|
| 195 |
+
sql.Identifier(table_name),
|
| 196 |
+
sql.SQL(', ').join(map(sql.Identifier, columns))
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
# Load data from CSV file into the table
|
| 201 |
+
with open(csv_file_path, 'r') as f:
|
| 202 |
+
cur.copy_expert(sql=copy_sql, file=f)
|
| 203 |
+
# conn.commit()
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
# Commit the transaction
|
| 207 |
+
conn.commit()
|
| 208 |
+
|
| 209 |
+
cur.close()
|
| 210 |
+
conn.close()
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
# In[150]:
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
if os.path.exists('data.csv'):
|
| 217 |
+
os.remove(csv_file_path)
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
# In[ ]:
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
|