Alex Ndubuisi commited on
Commit
534c972
·
1 Parent(s): 83f5ac5

Update space

Browse files
Files changed (4) hide show
  1. .env +2 -0
  2. app.py +18 -3
  3. database.py +47 -0
  4. requirements.txt +5 -1
.env ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ DATABASE_URL=postgresql://Maskbook_owner:Uqfv0GgKD7tO@ep-curly-queen-a29g90ny.eu-central-1.aws.neon.tech/Maskbook?sslmode=require
2
+ FLASK_SECRET_KEY=SecretkeyRidimz24@22#
app.py CHANGED
@@ -1,4 +1,6 @@
1
  import gradio as gr
 
 
2
  from huggingface_hub import InferenceClient
3
 
4
  """
@@ -6,6 +8,20 @@ For more information on `huggingface_hub` Inference API support, please check th
6
  """
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def respond(
11
  message,
@@ -46,7 +62,7 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
  gr.Slider(
@@ -59,6 +75,5 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
  import gradio as gr
2
+ import psycopg2
3
+ from database import db_connection
4
  from huggingface_hub import InferenceClient
5
 
6
  """
 
8
  """
9
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
 
11
+ CREATOR_NAME = "Your Name" # Define your creator name here
12
+
13
+ def fetch_system_message():
14
+ conn = db_connection()
15
+ cursor = conn.cursor()
16
+ cursor.execute("SELECT message FROM system_messages WHERE name = 'MicroAI'")
17
+ result = cursor.fetchone()
18
+ cursor.close()
19
+ conn.close()
20
+
21
+ if result:
22
+ return result[0] # Return the message associated with 'MicroAI'
23
+ else:
24
+ return f"You are Micro AI, created by {CREATOR_NAME}. Follow the instructions precisely."
25
 
26
  def respond(
27
  message,
 
62
  demo = gr.ChatInterface(
63
  respond,
64
  additional_inputs=[
65
+ gr.Textbox(value=fetch_system_message(), label="System message"), # Call the function to fetch system message
66
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
67
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
68
  gr.Slider(
 
75
  ],
76
  )
77
 
 
78
  if __name__ == "__main__":
79
+ demo.launch()
database.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import psycopg2
2
+ from decouple import config
3
+ from urllib.parse import urlparse
4
+ import re
5
+ import sys
6
+ import shutil
7
+ import threading
8
+ import time
9
+ import os
10
+ import time
11
+ from datetime import datetime, timedelta, datetime
12
+ from colorama import Fore, Style, Back, init
13
+ init(autoreset=True)
14
+
15
+ def db_connection():
16
+ """Establishes a connection to the database with SSL and keepalives."""
17
+ # Parse DATABASE_URL
18
+ url = urlparse(config("DATABASE_URL"))
19
+
20
+ # Extract connection details from the DATABASE_URL
21
+ host = url.hostname
22
+ port = url.port
23
+ user = url.username
24
+ password = url.password
25
+ database = url.path[1:]
26
+
27
+ try:
28
+ # Connect using individual parameters
29
+ conn = psycopg2.connect(
30
+ host=host,
31
+ port=port,
32
+ user=user,
33
+ password=password,
34
+ dbname=database,
35
+ sslmode="require", # Use SSL
36
+ keepalives=1, # Enable keepalives
37
+ keepalives_idle=300, # Set idle time before keepalives (in seconds)
38
+ keepalives_interval=60, # Interval for keepalives (in seconds)
39
+ keepalives_count=5 # Number of keepalive retries before disconnecting
40
+ )
41
+
42
+ return conn
43
+ except psycopg2.OperationalError as db_error:
44
+ return
45
+ except Exception as e:
46
+ print(f"Database connection error: {e}"), 500
47
+ return conn
requirements.txt CHANGED
@@ -1 +1,5 @@
1
- huggingface_hub==0.25.2
 
 
 
 
 
1
+ gradio
2
+ huggingface_hub
3
+ psycopg2
4
+ colorama
5
+ python-decouple