theabdulsaboor commited on
Commit
8ebeb01
·
verified ·
1 Parent(s): 3261c86

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import os
4
+ import PyPDF2
5
+
6
+ # Sidebar components
7
+ st.sidebar.title("TherapyGPT Settings")
8
+
9
+ # Model selection dropdown
10
+ model_option = st.sidebar.selectbox(
11
+ "Choose AI Model:",
12
+ ("GPT-4o", "distilgpt2", "gpt-neo")
13
+ )
14
+
15
+ # Document upload
16
+ uploaded_file = st.sidebar.file_uploader(
17
+ "Upload Document", type=["txt", "pdf"])
18
+
19
+ # Workspace/session management
20
+ workspace_name = st.sidebar.text_input("Workspace Name", value="Default")
21
+ if st.sidebar.button("Create Workspace"):
22
+ st.session_state.workspace = workspace_name
23
+ st.session_state.chat_history = [] # Reset chat history
24
+
25
+
26
+ # Main chat area
27
+ st.title("TherapyGPT Chat")
28
+
29
+ # Display chat history if it exists
30
+ if 'chat_history' not in st.session_state:
31
+ st.session_state.chat_history = []
32
+
33
+ for message in st.session_state.chat_history:
34
+ st.write(f"{message['role']}: {message['content']}")
35
+
36
+ # User input
37
+ user_input = st.text_input("You:", "")
38
+ if st.button("Send"):
39
+ if user_input:
40
+ # Placeholder for model interaction (we'll integrate this next)
41
+ st.session_state.chat_history.append(
42
+ {"role": "User", "content": user_input})
43
+ response = "I'm here to help." # Temporary placeholder
44
+ st.session_state.chat_history.append(
45
+ {"role": "TherapyGPT", "content": response})
46
+
47
+
48
+ # Load the model based on selection
49
+
50
+ @st.cache_resource
51
+ def load_model(model_name):
52
+ return pipeline("text-generation", model=model_name)
53
+
54
+
55
+ # Model selection logic
56
+ if model_option == "GPT-4o":
57
+ # Replace with appropriate model
58
+ model = load_model("ruslandev/llama-3-8b-gpt-4o-ru1.0-gguf")
59
+ elif model_option == "distilgpt2":
60
+ pipe = pipeline("text-generation", model="distilbert/distilgpt2")
61
+ else:
62
+ # Example HuggingFace model
63
+ pipe = pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B")
64
+
65
+
66
+ def read_file(file):
67
+ if file.name.endswith('.txt'):
68
+ return file.read().decode('utf-8')
69
+ elif file.name.endswith('.pdf'):
70
+ pdf_reader = PyPDF2.PdfReader(file)
71
+ return '\n'.join([page.extract_text() for page in pdf_reader.pages])
72
+
73
+
74
+ if uploaded_file:
75
+ file_content = read_file(uploaded_file)
76
+ st.sidebar.text_area("File Content", file_content, height=200)
77
+
78
+
79
+ if 'workspaces' not in st.session_state:
80
+ st.session_state.workspaces = {}
81
+
82
+ # Load or create the workspace
83
+ if workspace_name not in st.session_state.workspaces:
84
+ st.session_state.workspaces[workspace_name] = []
85
+
86
+ # Use the chat history for this workspace
87
+ chat_history = st.session_state.workspaces[workspace_name]