cevheri commited on
Commit
d9d895f
·
1 Parent(s): 0d97da0

feat: HF deployment encahcement

Browse files
Files changed (2) hide show
  1. gradio_chatbot.py +35 -29
  2. main.py +10 -5
gradio_chatbot.py CHANGED
@@ -12,8 +12,14 @@ from loguru import logger
12
  env = environs.Env()
13
  env.read_env()
14
 
 
 
 
 
15
  # API Configuration
16
  BASE_URL = env.str("BASE_URL", "http://localhost:7860")
 
 
17
  API_KEY = env.str("API_KEY", "sk-test-xxx")
18
  CHAT_API_ENDPOINT = f"{BASE_URL}/v1/chat/completions"
19
 
@@ -26,79 +32,79 @@ USER_AVATAR = os.path.join(AVATAR_DIR, "user.png")
26
  BOT_AVATAR = os.path.join(AVATAR_DIR, "bot.png")
27
 
28
  # Custom CSS for fonts
29
- CUSTOM_CSS = """
30
- @font-face {
31
  font-family: 'UI Sans Serif';
32
- src: url('/static/fonts/ui-sans-serif/ui-sans-serif-Regular.woff2') format('woff2');
33
  font-weight: normal;
34
  font-style: normal;
35
- }
36
 
37
- @font-face {
38
  font-family: 'UI Sans Serif';
39
- src: url('/static/fonts/ui-sans-serif/ui-sans-serif-Bold.woff2') format('woff2');
40
  font-weight: bold;
41
  font-style: normal;
42
- }
43
 
44
- @font-face {
45
  font-family: 'System UI';
46
- src: url('/static/fonts/system-ui/system-ui-Regular.woff2') format('woff2');
47
  font-weight: normal;
48
  font-style: normal;
49
- }
50
 
51
- @font-face {
52
  font-family: 'System UI';
53
- src: url('/static/fonts/system-ui/system-ui-Bold.woff2') format('woff2');
54
  font-weight: bold;
55
  font-style: normal;
56
- }
57
 
58
- .gradio-container {
59
  font-family: 'UI Sans Serif', 'System UI', sans-serif;
60
- }
61
 
62
  /* Improve chat interface */
63
- .chat-message {
64
  padding: 1rem;
65
  border-radius: 0.5rem;
66
  margin-bottom: 1rem;
67
  display: flex;
68
  align-items: flex-start;
69
- }
70
 
71
- .chat-message.user {
72
  background-color: #f3f4f6;
73
- }
74
 
75
- .chat-message.bot {
76
  background-color: #eef2ff;
77
- }
78
 
79
  /* Improve button styles */
80
- button {
81
  transition: all 0.2s ease-in-out;
82
- }
83
 
84
- button:hover {
85
  transform: translateY(-1px);
86
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
87
- }
88
 
89
  /* Improve input area */
90
- textarea {
91
  border-radius: 0.5rem;
92
  padding: 0.75rem;
93
  border: 1px solid #e5e7eb;
94
  transition: border-color 0.2s ease-in-out;
95
- }
96
 
97
- textarea:focus {
98
  border-color: #4f46e5;
99
  outline: none;
100
  box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.1);
101
- }
102
  """
103
 
104
  class MessageStatus(Enum):
 
12
  env = environs.Env()
13
  env.read_env()
14
 
15
+ # Hugging Face Spaces için özel yapılandırma
16
+ IS_HF_SPACE = os.environ.get("SPACE_ID") is not None
17
+ BASE_PATH = "" if not IS_HF_SPACE else "/spaces/lokumai/openai-openapi-template"
18
+
19
  # API Configuration
20
  BASE_URL = env.str("BASE_URL", "http://localhost:7860")
21
+ if IS_HF_SPACE:
22
+ BASE_URL = f"https://huggingface.co{BASE_PATH}"
23
  API_KEY = env.str("API_KEY", "sk-test-xxx")
24
  CHAT_API_ENDPOINT = f"{BASE_URL}/v1/chat/completions"
25
 
 
32
  BOT_AVATAR = os.path.join(AVATAR_DIR, "bot.png")
33
 
34
  # Custom CSS for fonts
35
+ CUSTOM_CSS = f"""
36
+ @font-face {{
37
  font-family: 'UI Sans Serif';
38
+ src: url('{BASE_PATH}/static/fonts/ui-sans-serif/ui-sans-serif-Regular.woff2') format('woff2');
39
  font-weight: normal;
40
  font-style: normal;
41
+ }}
42
 
43
+ @font-face {{
44
  font-family: 'UI Sans Serif';
45
+ src: url('{BASE_PATH}/static/fonts/ui-sans-serif/ui-sans-serif-Bold.woff2') format('woff2');
46
  font-weight: bold;
47
  font-style: normal;
48
+ }}
49
 
50
+ @font-face {{
51
  font-family: 'System UI';
52
+ src: url('{BASE_PATH}/static/fonts/system-ui/system-ui-Regular.woff2') format('woff2');
53
  font-weight: normal;
54
  font-style: normal;
55
+ }}
56
 
57
+ @font-face {{
58
  font-family: 'System UI';
59
+ src: url('{BASE_PATH}/static/fonts/system-ui/system-ui-Bold.woff2') format('woff2');
60
  font-weight: bold;
61
  font-style: normal;
62
+ }}
63
 
64
+ .gradio-container {{
65
  font-family: 'UI Sans Serif', 'System UI', sans-serif;
66
+ }}
67
 
68
  /* Improve chat interface */
69
+ .chat-message {{
70
  padding: 1rem;
71
  border-radius: 0.5rem;
72
  margin-bottom: 1rem;
73
  display: flex;
74
  align-items: flex-start;
75
+ }}
76
 
77
+ .chat-message.user {{
78
  background-color: #f3f4f6;
79
+ }}
80
 
81
+ .chat-message.bot {{
82
  background-color: #eef2ff;
83
+ }}
84
 
85
  /* Improve button styles */
86
+ button {{
87
  transition: all 0.2s ease-in-out;
88
+ }}
89
 
90
+ button:hover {{
91
  transform: translateY(-1px);
92
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
93
+ }}
94
 
95
  /* Improve input area */
96
+ textarea {{
97
  border-radius: 0.5rem;
98
  padding: 0.75rem;
99
  border: 1px solid #e5e7eb;
100
  transition: border-color 0.2s ease-in-out;
101
+ }}
102
 
103
+ textarea:focus {{
104
  border-color: #4f46e5;
105
  outline: none;
106
  box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.1);
107
+ }}
108
  """
109
 
110
  class MessageStatus(Enum):
main.py CHANGED
@@ -10,6 +10,7 @@ from contextlib import asynccontextmanager
10
  from app.db.client import mongodb
11
  from gradio_chatbot import build_gradio_app
12
  import gradio as gr
 
13
 
14
  print(log_config.get_log_level())
15
 
@@ -18,6 +19,10 @@ env.read_env()
18
 
19
  STORAGE_TYPE = env.str("STORAGE_TYPE", "mongodb")
20
 
 
 
 
 
21
  @asynccontextmanager
22
  async def lifespan(app: FastAPI):
23
  """
@@ -35,7 +40,6 @@ async def lifespan(app: FastAPI):
35
  if STORAGE_TYPE == "mongodb":
36
  await mongodb.close()
37
 
38
-
39
  VERSION = "0.3.0"
40
  TITLE = "Talk to your data chat and visualize API"
41
  DESCRIPTION = """
@@ -76,6 +80,7 @@ app = FastAPI(
76
  lifespan= lifespan,
77
  openapi_tags= openapi_tags,
78
  debug= True,
 
79
  )
80
 
81
  # Configure OpenAPI security scheme
@@ -110,7 +115,7 @@ app = gr.mount_gradio_app(app, demo, path="/ui")
110
  @app.get("/")
111
  async def root():
112
  """Redirect root to Gradio UI"""
113
- return RedirectResponse(url="/ui")
114
 
115
  @app.get("/manifest.json")
116
  async def get_manifest():
@@ -119,18 +124,18 @@ async def get_manifest():
119
  "name": "Data Chatbot",
120
  "short_name": "Chatbot",
121
  "description": "A chatbot interface for data visualization and analysis",
122
- "start_url": "/ui",
123
  "display": "standalone",
124
  "background_color": "#ffffff",
125
  "theme_color": "#4f46e5",
126
  "icons": [
127
  {
128
- "src": "/static/icons/icon-192x192.png",
129
  "sizes": "192x192",
130
  "type": "image/png"
131
  },
132
  {
133
- "src": "/static/icons/icon-512x512.png",
134
  "sizes": "512x512",
135
  "type": "image/png"
136
  }
 
10
  from app.db.client import mongodb
11
  from gradio_chatbot import build_gradio_app
12
  import gradio as gr
13
+ import os
14
 
15
  print(log_config.get_log_level())
16
 
 
19
 
20
  STORAGE_TYPE = env.str("STORAGE_TYPE", "mongodb")
21
 
22
+ # Hugging Face Spaces için özel yapılandırma
23
+ IS_HF_SPACE = os.environ.get("SPACE_ID") is not None
24
+ BASE_PATH = "" if not IS_HF_SPACE else "/spaces/lokumai/openai-openapi-template"
25
+
26
  @asynccontextmanager
27
  async def lifespan(app: FastAPI):
28
  """
 
40
  if STORAGE_TYPE == "mongodb":
41
  await mongodb.close()
42
 
 
43
  VERSION = "0.3.0"
44
  TITLE = "Talk to your data chat and visualize API"
45
  DESCRIPTION = """
 
80
  lifespan= lifespan,
81
  openapi_tags= openapi_tags,
82
  debug= True,
83
+ root_path=BASE_PATH
84
  )
85
 
86
  # Configure OpenAPI security scheme
 
115
  @app.get("/")
116
  async def root():
117
  """Redirect root to Gradio UI"""
118
+ return RedirectResponse(url=f"{BASE_PATH}/ui")
119
 
120
  @app.get("/manifest.json")
121
  async def get_manifest():
 
124
  "name": "Data Chatbot",
125
  "short_name": "Chatbot",
126
  "description": "A chatbot interface for data visualization and analysis",
127
+ "start_url": f"{BASE_PATH}/ui",
128
  "display": "standalone",
129
  "background_color": "#ffffff",
130
  "theme_color": "#4f46e5",
131
  "icons": [
132
  {
133
+ "src": f"{BASE_PATH}/static/icons/icon-192x192.png",
134
  "sizes": "192x192",
135
  "type": "image/png"
136
  },
137
  {
138
+ "src": f"{BASE_PATH}/static/icons/icon-512x512.png",
139
  "sizes": "512x512",
140
  "type": "image/png"
141
  }