Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from groq import Groq
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
st.write("""TESV chat with Text and Csv files
|
7 |
+
|
8 |
+
by theaimart
|
9 |
+
""")
|
10 |
+
|
11 |
+
api_key = "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"
|
12 |
+
|
13 |
+
if 'chat_history' not in st.session_state:
|
14 |
+
st.session_state.chat_history = []
|
15 |
+
if 'uploaded_files' not in st.session_state:
|
16 |
+
st.session_state.uploaded_files = []
|
17 |
+
if 'file_contents' not in st.session_state:
|
18 |
+
st.session_state.file_contents = []
|
19 |
+
|
20 |
+
title = st.chat_input("Ask TESV anything!!!")
|
21 |
+
|
22 |
+
if title:
|
23 |
+
client = Groq(
|
24 |
+
api_key="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"
|
25 |
+
)
|
26 |
+
# Include file contents in the prompt if any
|
27 |
+
prompt = st.session_state.chat_history + [{"role": "system", "content": content} for content in st.session_state.file_contents]
|
28 |
+
|
29 |
+
prompt.append({
|
30 |
+
"role": "user",
|
31 |
+
"content": title,
|
32 |
+
})
|
33 |
+
chat_completion = client.chat.completions.create(
|
34 |
+
messages=prompt,
|
35 |
+
model="llama3-8b-8192",
|
36 |
+
)
|
37 |
+
st.session_state.chat_history.append({
|
38 |
+
"role": "user",
|
39 |
+
"content": title,
|
40 |
+
})
|
41 |
+
st.session_state.chat_history.append({
|
42 |
+
"role": "assistant",
|
43 |
+
"content": chat_completion.choices[0].message.content,
|
44 |
+
})
|
45 |
+
|
46 |
+
uploaded_file = st.file_uploader("Choose a file")
|
47 |
+
if uploaded_file is not None:
|
48 |
+
file_name = uploaded_file.name
|
49 |
+
if file_name not in st.session_state.uploaded_files:
|
50 |
+
st.session_state.uploaded_files.append(file_name)
|
51 |
+
file_extension = file_name.split('.')[-1]
|
52 |
+
|
53 |
+
if file_extension == 'csv':
|
54 |
+
# Read CSV file into a DataFrame
|
55 |
+
dataframe = pd.read_csv(uploaded_file)
|
56 |
+
st.session_state.file_contents.append(dataframe.to_string(index=False))
|
57 |
+
elif file_extension == 'txt':
|
58 |
+
# Read text file content
|
59 |
+
string_data = uploaded_file.read().decode("utf-8")
|
60 |
+
st.session_state.file_contents.append(string_data)
|
61 |
+
else:
|
62 |
+
st.error("Unsupported file type")
|
63 |
+
|
64 |
+
# Display the chat history and file contents
|
65 |
+
for message in st.session_state.chat_history:
|
66 |
+
if message["role"] == "user":
|
67 |
+
with st.chat_message("user"):
|
68 |
+
st.write(message["content"])
|
69 |
+
elif message["role"] == "assistant":
|
70 |
+
with st.chat_message("assistant"):
|
71 |
+
st.write(message["content"])
|
72 |
+
elif message["role"] == "file":
|
73 |
+
st.text(message["content"])
|
74 |
+
|
75 |
+
# Add GitHub and LinkedIn links at the bottom left
|
76 |
+
st.markdown(
|
77 |
+
"""
|
78 |
+
<style>
|
79 |
+
.fixed-footer {
|
80 |
+
position: fixed;
|
81 |
+
bottom: 10px;
|
82 |
+
left: 10px;
|
83 |
+
width: auto;
|
84 |
+
background-color: white;
|
85 |
+
text-align: left;
|
86 |
+
padding: 10px;
|
87 |
+
border: 1px solid #ddd;
|
88 |
+
border-radius: 5px;
|
89 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
|
90 |
+
z-index: 100;
|
91 |
+
}
|
92 |
+
</style>
|
93 |
+
|
94 |
+
""",
|
95 |
+
unsafe_allow_html=True
|
96 |
+
)
|