Devis2awe commited on
Commit
95d6a95
·
verified ·
1 Parent(s): f9005d0

Create app

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ from gtts import gTTS
3
+ from io import BytesIO,StringIO
4
+ #Using streamlit app framework
5
+ import streamlit as st
6
+
7
+
8
+
9
+
10
+ #Creating client
11
+ client= OpenAI(
12
+ api_key="sk-1234567890",
13
+ base_url='http://localhost:8000/v1'
14
+ )
15
+
16
+
17
+ # Title of the app
18
+ st.title("TherapyBot- A Chatbot for Mental Health Support")
19
+
20
+
21
+
22
+
23
+ #Uploading medical records
24
+ uploaded_file = st.file_uploader("", type=["txt"], label_visibility="collapsed")
25
+ css = '''
26
+ <style>
27
+ [data-testid='stFileUploader'] {
28
+ width: max-content;
29
+ }
30
+ [data-testid='stFileUploader'] section {
31
+ padding: 0;
32
+ float: left;
33
+ }
34
+ [data-testid='stFileUploader'] section > input + div {
35
+ display: none;
36
+ }
37
+ [data-testid='stFileUploader'] section + div {
38
+ float: right;
39
+ padding-top: 0;
40
+ }
41
+
42
+ </style>
43
+ '''
44
+ st.markdown(css, unsafe_allow_html=True)
45
+
46
+ doc_data = ""
47
+ #If user uploads a file
48
+ if uploaded_file is not None:
49
+
50
+ # To read file as string:
51
+ stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
52
+ doc_data = stringio.read()
53
+ doc_data = "This is my medical record - "+doc_data+" Please answer the following question based on the earlier medical record- "
54
+
55
+
56
+
57
+ #Taking user input
58
+ prompt= st.chat_input('Chat to start or upload any medical records. How can I help?')
59
+
60
+
61
+ #If users types prompt and hits enter
62
+ if prompt:
63
+ st.chat_message("user").markdown(prompt)
64
+ #Chat Completion
65
+ reponse= client.chat.completions.create(
66
+ #which model we wanna use
67
+ model='D:\CHATBOT_PROJ_NEW\MentaLLaMA-chat-7b-GGUF-q8\MentaLLaMA-chat-7b-GGUF-q8.gguf',
68
+ #pass through the prompt
69
+ messages=[{
70
+ 'role': 'user',
71
+ 'content': doc_data+prompt
72
+ }],
73
+ #Add streaming
74
+ stream=True
75
+ )
76
+ with st.chat_message("ai"):
77
+ completed_message = ""
78
+ message= st.empty()
79
+ #Streaming the response out
80
+ for chunk in reponse:
81
+ if chunk.choices[0].delta.content is not None:
82
+ #print(chunk.choices[0].delta.content, flush=True, end="")
83
+ completed_message += chunk.choices[0].delta.content
84
+ message.markdown(completed_message)
85
+
86
+ sound_file = BytesIO()
87
+ tts = gTTS(completed_message, lang='en')
88
+ tts.write_to_fp(sound_file)
89
+ st.audio(sound_file)
90
+
91
+