jeysshon commited on
Commit
6af0dac
·
verified ·
1 Parent(s): ca545d6

Upload 5 files

Browse files
Files changed (5) hide show
  1. README.md +6 -6
  2. app.py +133 -0
  3. gitattributes +35 -0
  4. requirements.txt +7 -0
  5. style.css +19 -0
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: CJMC
3
- emoji: 😻
4
- colorFrom: yellow
5
- colorTo: gray
6
- sdk: gradio
7
- sdk_version: 4.43.0
8
  app_file: app.py
9
  pinned: false
10
  ---
 
1
  ---
2
+ title: Chatbot
3
+ emoji:
4
+ colorFrom: purple
5
+ colorTo: red
6
+ sdk: streamlit
7
+ sdk_version: 1.37.1
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import speech_recognition as sr
4
+ import google.generativeai as genai
5
+ from pymongo import MongoClient
6
+ from bson.objectid import ObjectId
7
+ import pyttsx3
8
+
9
+ client = MongoClient("mongodb://localhost:27017/")
10
+ db = client["search_history"]
11
+ history_collection = db["history"]
12
+
13
+ api_key = 'AIzaSyDJZ3r6VRhRivR0pb96cBRg_VvGg_fXq5k'
14
+ params = {
15
+ 'key': api_key,
16
+ }
17
+
18
+ def save_history(input_mode, user_input, response):
19
+ history_entry = {
20
+ "input_mode": input_mode,
21
+ "user_input": user_input,
22
+ "response": response
23
+ }
24
+ history_collection.insert_one(history_entry)
25
+
26
+ def load_history():
27
+ history_entries = list(history_collection.find().sort("_id", -1))
28
+ return history_entries
29
+
30
+ def delete_history(entry_id):
31
+ history_collection.delete_one({"_id": ObjectId(entry_id)})
32
+
33
+ def process_text(text):
34
+ genai.configure(api_key=api_key)
35
+ model = genai.GenerativeModel('gemini-1.5-pro-latest')
36
+ response = model.generate_content(text)
37
+ result = response.text
38
+ save_history("Text", text, result)
39
+ return result
40
+
41
+ def process_image(text):
42
+ genai.configure(api_key=api_key)
43
+ model = genai.GenerativeModel('gemini-1.5-pro-latest')
44
+ response = model.generate_content(text)
45
+ save_history("Image", image.name, response)
46
+ return response
47
+
48
+ def recognize_speech():
49
+ recognizer = sr.Recognizer()
50
+ with sr.Microphone() as source:
51
+ st.write("Listening...")
52
+ audio = recognizer.listen(source)
53
+ try:
54
+ text = recognizer.recognize_google(audio)
55
+ return text
56
+ except sr.UnknownValueError:
57
+ return "Google Speech Recognition could not understand audio"
58
+ except sr.RequestError as e:
59
+ return f"Could not request results from Google Speech Recognition service; {e}"
60
+
61
+ def speak_text(text):
62
+ engine = pyttsx3.init()
63
+ engine.say(text)
64
+ engine.runAndWait()
65
+
66
+ st.set_page_config(layout="wide")
67
+ st.sidebar.title("History")
68
+
69
+ history = load_history()
70
+ selected_response = None
71
+
72
+ for entry in history:
73
+ with st.sidebar.expander(f"{entry['user_input']}"):
74
+ col1, col2 = st.columns([1, 1])
75
+ with col1:
76
+ if st.button("Response", key=str(entry['_id'])):
77
+ selected_response = entry['response']
78
+ with col2:
79
+ if st.button("Delete", key=f"delete_{entry['_id']}"):
80
+ delete_history(entry['_id'])
81
+ st.experimental_rerun()
82
+
83
+ st.title("🤖 ChatBot")
84
+
85
+ generated_content_placeholder = st.empty()
86
+
87
+ if selected_response:
88
+ generated_content_placeholder.write(selected_response)
89
+ if st.button("🔊 Speak"):
90
+ speak_text(selected_response)
91
+
92
+ # Load and apply custom CSS
93
+ with open("./style.css") as f:
94
+ st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
95
+
96
+
97
+ # Main content
98
+ col1, col2 = st.columns([1, 3])
99
+
100
+ with col1:
101
+ input_type = st.selectbox("Select input type", ["Ask a Question❓", "🖼️ upload Image", "🎤 use Microphone"])
102
+
103
+ with col2:
104
+ if input_type == "Ask a Question❓":
105
+ text_input = st.text_input("Enter your prompt here")
106
+ if text_input:
107
+ with st.spinner("Generating response..."):
108
+ result = process_text(text_input)
109
+ generated_content_placeholder.write(result)
110
+ if st.button("🔊 Speak", key="speak_text_input"):
111
+ speak_text(result)
112
+
113
+ elif input_type == "🖼️ upload Image":
114
+ image_input = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
115
+ if image_input:
116
+ image = Image.open(image_input)
117
+ st.image(image, caption='Uploaded Image.', use_column_width=True)
118
+ with st.spinner("Processing image..."):
119
+ response = process_text(image_input)
120
+ generated_content_placeholder.write(response)
121
+
122
+ elif input_type == "🎤 use Microphone":
123
+ if st.button("Record"):
124
+ with st.spinner("Listening and processing..."):
125
+ text_from_speech = recognize_speech()
126
+ if text_from_speech:
127
+ text_input = st.text_input("Speak", value=text_from_speech)
128
+ if text_input:
129
+ with st.spinner("Generating response..."):
130
+ result = process_text(text_input)
131
+ generated_content_placeholder.write(result)
132
+ if st.button("🔊 Speak", key="speak_speech_input"):
133
+ speak_text(result)
gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ Pillow
3
+ SpeechRecognition
4
+ google-generativeai
5
+ pymongo
6
+ pyttsx3
7
+ bson
style.css ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .footer-container {
2
+ position: fixed;
3
+ bottom: 0;
4
+ width: 100%;
5
+ background-color: white;
6
+ padding: 10px;
7
+ border-top: 1px solid #eaeaea;
8
+ z-index: 1000;
9
+ }
10
+
11
+ .footer {
12
+ text-align: center;
13
+ padding: 10px;
14
+ font-size: 14px;
15
+ }
16
+
17
+ .stSelectbox, .stTextInput, .stButton {
18
+ margin-bottom: 0 !important;
19
+ }