arittrabag commited on
Commit
42b4aee
·
verified ·
1 Parent(s): d189e38

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +2 -8
  2. auth.py +76 -0
  3. chatbot.py +56 -0
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: RadicalX GenAI Chatbot
3
- emoji: 📊
4
- colorFrom: gray
5
- colorTo: pink
6
  sdk: gradio
7
  sdk_version: 4.14.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: RadicalX_GenAI_Chatbot
3
+ app_file: chatbot.py
 
 
4
  sdk: gradio
5
  sdk_version: 4.14.0
 
 
6
  ---
 
 
auth.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+
5
+ api_key = "AIzaSyDP2S0msFLcdaebwQqeZsfalL-kDRWSmYs"
6
+ api_url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent"
7
+
8
+ memory = {}
9
+ users = {} # Store user credentials
10
+
11
+ def generate_content(prompt):
12
+ headers = {'Content-Type': 'application/json'}
13
+ data = {
14
+ "contents": [
15
+ {
16
+ "parts": [
17
+ {"text": prompt}
18
+ ]
19
+ }
20
+ ]
21
+ }
22
+ params = {'key': api_key}
23
+
24
+ response = requests.post(api_url, headers=headers, data=json.dumps(data), params=params)
25
+
26
+ if response.status_code == 200:
27
+ try:
28
+ candidates = response.json().get('candidates', [])
29
+ if candidates:
30
+ content = candidates[0].get('content', {}).get('parts', [])[0].get('text', '')
31
+ return content
32
+ else:
33
+ return f"Error: 'candidates' not found in response"
34
+ except (IndexError, KeyError):
35
+ return f"Error: Unexpected response structure - {response.json()}"
36
+ else:
37
+ return f"Error: {response.status_code}, {response.text}"
38
+
39
+ def authenticate_user(username, password):
40
+ # Simple username-password authentication
41
+ return users.get(username) == password
42
+
43
+ def chatbot(user_input, username, password, action):
44
+ global memory
45
+ if 'history' not in memory:
46
+ memory['history'] = []
47
+
48
+ if action == 'signup':
49
+ # Sign up new user
50
+ if username not in users:
51
+ users[username] = password
52
+ return "Signup successful. You can now log in and start chatting."
53
+ else:
54
+ return "Error: Username already exists. Choose a different username."
55
+
56
+ elif action == 'login':
57
+ # Log in existing user
58
+ if authenticate_user(username, password):
59
+ # Remember user input
60
+ memory['history'].append(user_input)
61
+
62
+ # Generate response based on user input and history
63
+ prompt = ' '.join(memory['history']) # Combine previous inputs
64
+ response = generate_content(prompt)
65
+
66
+ # Remember generated response
67
+ memory['history'].append(response)
68
+
69
+ return response
70
+ else:
71
+ return "Error: Invalid username or password. Please try again."
72
+
73
+ # Define Gradio interface with authentication
74
+ iface = gr.Interface(fn=chatbot, inputs=["text", "text", "password", "text"], outputs="text",
75
+ live=True, auth=("username", "password"))
76
+ iface.launch()
chatbot.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+
5
+ api_key = "AIzaSyDP2S0msFLcdaebwQqeZsfalL-kDRWSmYs"
6
+ api_url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent"
7
+
8
+ memory = {}
9
+
10
+ def generate_content(prompt):
11
+ headers = {'Content-Type': 'application/json'}
12
+ data = {
13
+ "contents": [
14
+ {
15
+ "parts": [
16
+ {"text": prompt}
17
+ ]
18
+ }
19
+ ]
20
+ }
21
+ params = {'key': api_key}
22
+
23
+ response = requests.post(api_url, headers=headers, data=json.dumps(data), params=params)
24
+
25
+ if response.status_code == 200:
26
+ try:
27
+ candidates = response.json().get('candidates', [])
28
+ if candidates:
29
+ content = candidates[0].get('content', {}).get('parts', [])[0].get('text', '')
30
+ return content
31
+ else:
32
+ return f"Error: 'candidates' not found in response"
33
+ except (IndexError, KeyError):
34
+ return f"Error: Unexpected response structure - {response.json()}"
35
+ else:
36
+ return f"Error: {response.status_code}, {response.text}"
37
+
38
+ def chatbot(input, timestamp):
39
+ global memory
40
+ if 'history' not in memory:
41
+ memory['history'] = []
42
+
43
+ # Remember user input
44
+ memory['history'].append(input)
45
+
46
+ # Generate response based on user input and history
47
+ prompt = ' '.join(memory['history']) # Combine previous inputs
48
+ response = generate_content(prompt)
49
+
50
+ # Remember generated response
51
+ memory['history'].append(response)
52
+
53
+ return response
54
+
55
+ # Define Gradio interface
56
+ gr.ChatInterface(chatbot).launch(share=True)