File size: 2,956 Bytes
4d5b43f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st          
import os
from groq import Groq
import pandas as pd

st.write("""TESV chat with Text and Csv files
         
by theaimart
        """)

api_key = "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"

if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []
if 'uploaded_files' not in st.session_state:
    st.session_state.uploaded_files = []
if 'file_contents' not in st.session_state:
    st.session_state.file_contents = []

title = st.chat_input("Ask TESV anything!!!")

if title:
    client = Groq(
        api_key="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"
    )
    # Include file contents in the prompt if any
    prompt = st.session_state.chat_history + [{"role": "system", "content": content} for content in st.session_state.file_contents]
    
    prompt.append({
                "role": "user",
                "content": title,
            })
    chat_completion = client.chat.completions.create(
        messages=prompt,
        model="llama3-8b-8192",
    )
    st.session_state.chat_history.append({
        "role": "user",
        "content": title,
    })
    st.session_state.chat_history.append({
                "role": "assistant",
                "content": chat_completion.choices[0].message.content,
            })

uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
    file_name = uploaded_file.name
    if file_name not in st.session_state.uploaded_files:
        st.session_state.uploaded_files.append(file_name)
        file_extension = file_name.split('.')[-1]
    
        if file_extension == 'csv':
            # Read CSV file into a DataFrame
            dataframe = pd.read_csv(uploaded_file)
            st.session_state.file_contents.append(dataframe.to_string(index=False))    
        elif file_extension == 'txt':
            # Read text file content
            string_data = uploaded_file.read().decode("utf-8")
            st.session_state.file_contents.append(string_data)
        else:
            st.error("Unsupported file type")

# Display the chat history and file contents
for message in st.session_state.chat_history:
    if message["role"] == "user":
        with st.chat_message("user"):
            st.write(message["content"])
    elif message["role"] == "assistant":
        with st.chat_message("assistant"):
            st.write(message["content"])
    elif message["role"] == "file":
        st.text(message["content"])

# Add GitHub and LinkedIn links at the bottom left
st.markdown(
    """
    <style>
    .fixed-footer {
        position: fixed;
        bottom: 10px;
        left: 10px;
        width: auto;
        background-color: white;
        text-align: left;
        padding: 10px;
        border: 1px solid #ddd;
        border-radius: 5px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
        z-index: 100;
    }
    </style>
    
    """,
    unsafe_allow_html=True
)