Spaces:
Sleeping
Sleeping
Create excel_chat.py
Browse files- excel_chat.py +58 -0
excel_chat.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from mistralai.client import MistralClient
|
3 |
+
from mistralai.models.chat_completion import ChatMessage
|
4 |
+
import os
|
5 |
+
import pandas as pd
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
def chat_with_mistral(source_cols, dest_col, prompt, tdoc_name, excel_file, url):
|
9 |
+
|
10 |
+
df = pd.read_excel(excel_file)
|
11 |
+
api_key = os.environ["MISTRAL_API_KEY"]
|
12 |
+
model = "mistral-small" # Use "Mistral-7B-v0.2" for "mistral-tiny"
|
13 |
+
|
14 |
+
|
15 |
+
client = MistralClient(api_key=api_key)
|
16 |
+
|
17 |
+
source_columns = source_cols#.split(", ") # Split input into multiple variables
|
18 |
+
df[dest_col] = ""
|
19 |
+
try:
|
20 |
+
file_name = url.split("/")[-2] + ".xlsx"
|
21 |
+
except:
|
22 |
+
file_name = excel_file
|
23 |
+
|
24 |
+
if tdoc_name != '':
|
25 |
+
filtered_df = df[df['File'] == tdoc_name]
|
26 |
+
if not filtered_df.empty:
|
27 |
+
concatenated_content = "\n\n".join(f"{column_name}: {filtered_df[column_name].iloc[0]}" for column_name in source_columns)
|
28 |
+
messages = [ChatMessage(role="user", content=f"Using the following content: {concatenated_content}"), ChatMessage(role="user", content=prompt)]
|
29 |
+
chat_response = client.chat(model=model, messages=messages)
|
30 |
+
filtered_df.loc[filtered_df.index[0], dest_col] = chat_response.choices[0].message.content
|
31 |
+
# Update the DataFrame with the modified row
|
32 |
+
df.update(filtered_df)
|
33 |
+
# Write the updated DataFrame to the Excel file
|
34 |
+
df.to_excel(file_name, index=False)
|
35 |
+
return file_name, df.head(5)
|
36 |
+
else:
|
37 |
+
return file_name, df.head(5)
|
38 |
+
else:
|
39 |
+
for index, row in df.iterrows():
|
40 |
+
concatenated_content = "\n\n".join(f"{column_name}: {row[column_name]}" for column_name in source_columns)
|
41 |
+
# Check if the concatenated content is not empty
|
42 |
+
print('test')
|
43 |
+
if not concatenated_content == "\n\n".join(f"{column_name}: nan" for column_name in source_columns):
|
44 |
+
print('c bon')
|
45 |
+
messages = [ChatMessage(role="user", content=f"Using the following content: {concatenated_content}"), ChatMessage(role="user", content=prompt)]
|
46 |
+
chat_response = client.chat(model=model, messages=messages)
|
47 |
+
df.at[index, dest_col] = chat_response.choices[0].message.content
|
48 |
+
|
49 |
+
df.to_excel(file_name, index=False)
|
50 |
+
return file_name, df.head(5)
|
51 |
+
|
52 |
+
|
53 |
+
def get_columns(file):
|
54 |
+
if file is not None:
|
55 |
+
df = pd.read_excel(file)
|
56 |
+
return gr.update(choices=list(df.columns)), df.head(5)
|
57 |
+
else:
|
58 |
+
return gr.update(choices=[]), pd.DataFrame()
|