GowthamYarlagadda commited on
Commit
c615b80
1 Parent(s): 365cfba

Upload 9 files

Browse files
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ my_new_api_key_here = AIzaSyBzmNeMK20wEVYKHfzy7QG_laJdnnx3pSw
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ .venv-gemini-bot/
2
+ python-version
3
+ __pycache__/
4
+ .python-version
README.md CHANGED
@@ -1,10 +1,21 @@
1
- ---
2
- title: Gemini Counsellor
3
- emoji: 📚
4
- colorFrom: red
5
- colorTo: red
6
- sdk: docker
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Gemini AI Chatbot
2
+
3
+
4
+ ### Install Dependencies
5
+ ```bash
6
+ pip install -r requirements.txt
7
+ ```
8
+ ### Run the Web App
9
+ In the terminal, execute the following command:
10
+
11
+ ```
12
+ python app.py
13
+ ```
14
+
15
+ This will launch a web app locally. Open your browser and navigate to the provided address (usually http://localhost:5000/) to interact with the Gemini AI chatbot.
16
+
17
+ Feel free to explore and customize the code according to your needs. Contributions are welcome!
18
+
19
+ **Notes**
20
+ * Ensure that you have Python installed on your system.
21
+ * Use a virtual environment for better isolation of dependencies.
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, redirect, url_for
2
+ import google.generativeai as genai
3
+
4
+ textsi_1 = """You simulate the human therapist experience of someone who has personal one-on-one sessions with his or her clients.
5
+ Chatting with you should feel like they are talking to a REAL therapist or Counsellor who knows how to listen carefully, be empathic, and analyse the situation.
6
+ Always respond with a short response. Always take time with the conversation so the client will have sufficient time to settle in.
7
+ Always remember that solving their problem is not your first preference! It\\\'s for the comfort of the client.
8
+ Greet them, Appreciate them for taking the step, talk to them, and close the conversation when they say bye. Just summarize the whole conversation and give advice points to the client to improve their situation in a better way!
9
+ Remember to set the mood of the conversation according to the client\\\'s mood."""
10
+
11
+
12
+ model = genai.GenerativeModel('gemini-1.5-flash-001', system_instruction=[textsi_1])
13
+
14
+ import os
15
+ my_api_key_gemini = os.getenv('my_new_api_key_here')
16
+
17
+ genai.configure(api_key=my_api_key_gemini)
18
+
19
+ app = Flask(__name__)
20
+
21
+ # Define your 404 error handler to redirect to the index page
22
+ @app.errorhandler(404)
23
+ def page_not_found(e):
24
+ return redirect(url_for('index'))
25
+
26
+ @app.route('/', methods=['POST', 'GET'])
27
+ def index():
28
+ if request.method == 'POST':
29
+ try:
30
+ prompt = request.form['prompt']
31
+ question = prompt
32
+
33
+ response = model.generate_content(question)
34
+
35
+ if response.text:
36
+ return response.text
37
+ else:
38
+ return "Sorry, but I think Gemini didn't want to answer that!"
39
+ except Exception as e:
40
+ return "Sorry, but Gemini didn't want to answer that!"
41
+
42
+ return render_template('index.html', **locals())
43
+
44
+ if __name__ == '__main__':
45
+ app.run(debug=True)
gunicorn_config.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ bind = '0.0.0.0:80'
2
+ workers = 2
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ Flask==2.2.3
2
+ google-generativeai
3
+ gunicorn==20.0.4
4
+ grpcio
5
+ Werkzeug==2.2.3
static/images/demo.gif ADDED
static/images/iba_logo.png ADDED
templates/index.html ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="utf-8">
6
+ <meta http-equiv="x-ua-compatible" content="ie=edge">
7
+ <meta name="viewport" content="width=device-width, initial-scale=1">
8
+ <meta name="description" content="Demonstration of Gemini API in a Python Flask Application.">
9
+
10
+ <title>IBA Chatbot</title>
11
+ <link rel="shortcut icon" type="image/x-icon" href="{{ url_for('static', filename='images/iba_logo.png') }}">
12
+ <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
13
+ integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
14
+ <link rel="preconnect" href="https://fonts.googleapis.com">
15
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
16
+ <link href="https://fonts.googleapis.com/css2?family=Manrope:wght@300;400;600&display=swap" rel="stylesheet">
17
+ <link rel="stylesheet" href="https://drive.google.com/uc?export=view&id=1yTLwNiCZhIdCWolQldwq4spHQkgZDqkG">
18
+ <style>
19
+ body {
20
+ font-family: 'Manrope', sans-serif;
21
+ margin: 0 auto 40px;
22
+ background-color: #101626;
23
+ color: #fff;
24
+ }
25
+
26
+ .skeleton1,
27
+ .skeleton2,
28
+ .skeleton3 {
29
+ min-height: 18px;
30
+ padding: 10px;
31
+ border-radius: 4px;
32
+ background-size: 40px 100%;
33
+ background-repeat: no-repeat;
34
+ background-position: left -40px top 0;
35
+ animation: shine 1s ease infinite;
36
+ }
37
+
38
+ .skeleton1 {
39
+ background-color: #101626;
40
+ width: 70%;
41
+ }
42
+
43
+ .skeleton2 {
44
+ background-color: #e2e5e7;
45
+ width: 50%;
46
+ }
47
+
48
+ .skeleton3 {
49
+ background-color: #e2e5e7;
50
+ width: 60%;
51
+ }
52
+
53
+ @keyframes shine {
54
+ to {
55
+ background-position: right -40px top 0;
56
+ }
57
+ }
58
+ </style>
59
+
60
+ </head>
61
+
62
+ <body class="w-lg-50 w-md-75 w-sm-100 py-3">
63
+ <main class="flex-shrink-0">
64
+ <div>
65
+ <br>
66
+ <br>
67
+ <h1 class="mt-3">Gemini AI Model - Free Chatbot</h1>
68
+ <p>This project is based on the free Gemini API recently released by Google. <span style="font-weight: bold;">60 queries per minute</span>. For more information, visit my <a href="https://medium.com/@fareedkhandev" target="_blank">Profile</a>.</p>
69
+
70
+ <br>
71
+
72
+ <div id="list-group" style="background-color: #101626;" class="list-group w-auto">
73
+ </div>
74
+
75
+ <div class="input-group w-lg-50 w-md-75 w-sm-100 p-3 fixed-bottom" style="margin: 0 auto; ">
76
+ <input type="text" class="form-control" id="chat-input"
77
+ style="background-color: #3A4556; border: #3A4556; color: #fff;">
78
+ <div class="input-group-append">
79
+ <button id="gpt-button"
80
+ style="background-color: #140b9d; color: white; border-radius: 0 5px 5px 0;" class="btn">Ask Gemini</button>
81
+ </div>
82
+ </div>
83
+ </div>
84
+ </main>
85
+
86
+ <script src="https://code.jquery.com/jquery-3.6.3.min.js"
87
+ integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
88
+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
89
+ integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous">
90
+ </script>
91
+
92
+ <script>
93
+ $("#gpt-button").click(function () {
94
+ var question = $("#chat-input").val();
95
+ let html_data = '';
96
+ html_data += `<a style="margin-top:-10px; background-color:#3A4556; border:none; color:white;"
97
+ class="list-group-item list-group-item-action d-flex gap-3 py-3">
98
+ <img
99
+ src="https://w7.pngwing.com/pngs/178/595/png-transparent-user-profile-computer-icons-login-user-avatars-thumbnail.png"
100
+ alt="twbs" width="32" height="32"
101
+ class="rounded-circle flex-shrink-0">
102
+ <div class="d-flex gap-2 w-100 justify-content-between">
103
+ <div>
104
+ <p class="mb-0 opacity-75">${question}</p>
105
+ </div>
106
+ </div>
107
+ </a>
108
+
109
+ <div id="response"></div>`;
110
+ $("#chat-input").val("");
111
+
112
+ $("#list-group").append(html_data);
113
+ $('div#response:last').html(`
114
+ <!-- code here -->
115
+ <div class="py-3" id="ok-good">
116
+ <h2 class="skeleton1"></h2>
117
+ <h2 class="skeleton2"></h2>
118
+ <h2 class="skeleton3"></h2>
119
+ </div>`);
120
+ if ($("#ok-good").length) {
121
+ $('html, body').animate({
122
+ scrollTop: $("#ok-good").offset().top
123
+ }, 100);
124
+ }
125
+
126
+ $.ajax({
127
+ type: "POST",
128
+ url: "/",
129
+ data: {
130
+ 'prompt': question
131
+ },
132
+ success: function (data) {
133
+ let gpt_data = '';
134
+ gpt_data += `<a style="background-color:#202835; border:black;" href="#"
135
+ class="list-group-item list-group-item-action d-flex gap-3 py-3">
136
+ <img src="{{ url_for('static', filename='images/iba_logo.png') }}" alt="twbs" width="32" height="32"
137
+ class="rounded-circle flex-shrink-0">
138
+ <div class="d-flex gap-2 w-100 justify-content-between">
139
+ <div>
140
+ <p id="typing-demo" class="mb-0 opacity-75 text-white"></p>
141
+ </div>
142
+ </div>
143
+ </a><br>`;
144
+
145
+ $("#list-group").append(gpt_data);
146
+ $('div#response:last').html('');
147
+
148
+ var i = 0;
149
+ var speed = 5;
150
+
151
+ function typeWriter() {
152
+ if (i < data.length) {
153
+ $("p#typing-demo:last").text($("p#typing-demo:last").text() + data.charAt(i));
154
+ i++;
155
+ setTimeout(typeWriter, speed);
156
+ }
157
+ }
158
+
159
+ typeWriter();
160
+ }
161
+ });
162
+ });
163
+ </script>
164
+ </body>
165
+
166
+ </html>