cevheri commited on
Commit
454d020
·
1 Parent(s): 3089d42

chore: add static files for gradio ui style

Browse files
gradio_chatbot.py CHANGED
@@ -5,6 +5,7 @@ import asyncio
5
  from typing import List, Tuple, Dict, Optional
6
  from dataclasses import dataclass
7
  from enum import Enum
 
8
 
9
  # Environment configuration
10
  env = environs.Env()
@@ -15,6 +16,49 @@ BASE_URL = env.str("BASE_URL", "http://localhost:7860")
15
  API_KEY = env.str("API_KEY", "sk-test-xxx")
16
  CHAT_API_ENDPOINT = f"{BASE_URL}/v1/chat/completions"
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  class MessageStatus(Enum):
19
  """Enum for message status"""
20
  SUCCESS = "Success"
@@ -107,7 +151,7 @@ class ChatInterface:
107
  Returns:
108
  gr.Blocks: The Gradio interface
109
  """
110
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
111
  # Header
112
  gr.Markdown(f"""
113
  # 🤖 Data Chatbot
@@ -126,7 +170,7 @@ class ChatInterface:
126
  label="Chat History",
127
  height=400,
128
  show_copy_button=True,
129
- avatar_images=("👤", "🤖")
130
  )
131
 
132
  # Message input area
 
5
  from typing import List, Tuple, Dict, Optional
6
  from dataclasses import dataclass
7
  from enum import Enum
8
+ import os
9
 
10
  # Environment configuration
11
  env = environs.Env()
 
16
  API_KEY = env.str("API_KEY", "sk-test-xxx")
17
  CHAT_API_ENDPOINT = f"{BASE_URL}/v1/chat/completions"
18
 
19
+ # Get absolute paths for static files
20
+ STATIC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static")
21
+ AVATAR_DIR = os.path.join(STATIC_DIR, "avatars")
22
+
23
+ # Avatar paths
24
+ USER_AVATAR = os.path.join(AVATAR_DIR, "user.png")
25
+ BOT_AVATAR = os.path.join(AVATAR_DIR, "bot.png")
26
+
27
+ # Custom CSS for fonts
28
+ CUSTOM_CSS = """
29
+ @font-face {
30
+ font-family: 'UI Sans Serif';
31
+ src: url('/static/fonts/ui-sans-serif/ui-sans-serif-Regular.woff2') format('woff2');
32
+ font-weight: normal;
33
+ font-style: normal;
34
+ }
35
+
36
+ @font-face {
37
+ font-family: 'UI Sans Serif';
38
+ src: url('/static/fonts/ui-sans-serif/ui-sans-serif-Bold.woff2') format('woff2');
39
+ font-weight: bold;
40
+ font-style: normal;
41
+ }
42
+
43
+ @font-face {
44
+ font-family: 'System UI';
45
+ src: url('/static/fonts/system-ui/system-ui-Regular.woff2') format('woff2');
46
+ font-weight: normal;
47
+ font-style: normal;
48
+ }
49
+
50
+ @font-face {
51
+ font-family: 'System UI';
52
+ src: url('/static/fonts/system-ui/system-ui-Bold.woff2') format('woff2');
53
+ font-weight: bold;
54
+ font-style: normal;
55
+ }
56
+
57
+ .gradio-container {
58
+ font-family: 'UI Sans Serif', 'System UI', sans-serif;
59
+ }
60
+ """
61
+
62
  class MessageStatus(Enum):
63
  """Enum for message status"""
64
  SUCCESS = "Success"
 
151
  Returns:
152
  gr.Blocks: The Gradio interface
153
  """
154
+ with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
155
  # Header
156
  gr.Markdown(f"""
157
  # 🤖 Data Chatbot
 
170
  label="Chat History",
171
  height=400,
172
  show_copy_button=True,
173
+ avatar_images=(USER_AVATAR, BOT_AVATAR)
174
  )
175
 
176
  # Message input area
main.py CHANGED
@@ -1,6 +1,7 @@
1
  from fastapi import FastAPI
2
- from fastapi.responses import RedirectResponse
3
  from fastapi.staticfiles import StaticFiles
 
4
  from app.api import chat_api
5
  from app.config.log import log_config
6
  from loguru import logger
@@ -89,16 +90,51 @@ app.openapi_components = {
89
  }
90
  }
91
 
 
 
 
 
 
 
 
 
 
92
  app.mount("/static", StaticFiles(directory="static"), name="static")
93
  app.mount("/.well-known", StaticFiles(directory=".well-known"), name="well-known")
94
  app.include_router(chat_api.router)
95
 
96
- # gradio mount in fastapi app
97
  demo = build_gradio_app()
98
  app = gr.mount_gradio_app(app, demo, path="/ui")
99
 
100
  @app.get("/")
101
  async def root():
102
- return RedirectResponse(url="http://localhost:7860/ui")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  # uv run uvicorn main:app --host 0.0.0.0 --port 7860 --reload
 
1
  from fastapi import FastAPI
2
+ from fastapi.responses import RedirectResponse, JSONResponse
3
  from fastapi.staticfiles import StaticFiles
4
+ from fastapi.middleware.cors import CORSMiddleware
5
  from app.api import chat_api
6
  from app.config.log import log_config
7
  from loguru import logger
 
90
  }
91
  }
92
 
93
+ # Configure CORS
94
+ app.add_middleware(
95
+ CORSMiddleware,
96
+ allow_origins=["*"],
97
+ allow_credentials=True,
98
+ allow_methods=["*"],
99
+ allow_headers=["*"],
100
+ )
101
+
102
  app.mount("/static", StaticFiles(directory="static"), name="static")
103
  app.mount("/.well-known", StaticFiles(directory=".well-known"), name="well-known")
104
  app.include_router(chat_api.router)
105
 
106
+ # Build and mount Gradio app
107
  demo = build_gradio_app()
108
  app = gr.mount_gradio_app(app, demo, path="/ui")
109
 
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():
117
+ """Return the web app manifest"""
118
+ return JSONResponse({
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
+ }
137
+ ]
138
+ })
139
 
140
  # uv run uvicorn main:app --host 0.0.0.0 --port 7860 --reload
static/avatars/bot.png ADDED
static/avatars/bot.svg ADDED
static/avatars/user.png ADDED
static/avatars/user.svg ADDED
static/fonts/system-ui/system-ui-Bold.woff2 ADDED
@@ -0,0 +1 @@
 
 
1
+ 404: Not Found
static/fonts/system-ui/system-ui-Regular.woff2 ADDED
@@ -0,0 +1 @@
 
 
1
+ 404: Not Found
static/fonts/ui-sans-serif/ui-sans-serif-Bold.woff2 ADDED
@@ -0,0 +1 @@
 
 
1
+ 404: Not Found
static/fonts/ui-sans-serif/ui-sans-serif-Regular.woff2 ADDED
@@ -0,0 +1 @@
 
 
1
+ 404: Not Found
static/icons/icon-192x192.png ADDED
static/icons/icon-192x192.svg ADDED
static/icons/icon-512x512.png ADDED
static/icons/icon-512x512.svg ADDED
static/manifest.json ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "Data Chatbot",
3
+ "short_name": "Chatbot",
4
+ "description": "A chatbot interface for data visualization and analysis",
5
+ "start_url": "/ui",
6
+ "display": "standalone",
7
+ "background_color": "#ffffff",
8
+ "theme_color": "#4f46e5",
9
+ "icons": [
10
+ {
11
+ "src": "/static/icons/icon-192x192.png",
12
+ "sizes": "192x192",
13
+ "type": "image/png"
14
+ },
15
+ {
16
+ "src": "/static/icons/icon-512x512.png",
17
+ "sizes": "512x512",
18
+ "type": "image/png"
19
+ }
20
+ ]
21
+ }