Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,6 @@ from huggingface_hub import login
|
|
8 |
# Space configuration
|
9 |
SPACE_DIR = os.environ.get("HF_HOME", os.getcwd())
|
10 |
|
11 |
-
|
12 |
def init_huggingface_auth():
|
13 |
"""Space-friendly authentication"""
|
14 |
token = os.getenv("HUGGINGFACE_TOKEN")
|
@@ -28,15 +27,12 @@ def init_huggingface_auth():
|
|
28 |
if not init_huggingface_auth():
|
29 |
print("Warning: Authentication failed")
|
30 |
|
31 |
-
|
32 |
# Load and preprocess the PDF content
|
33 |
pdf_path = os.path.join(SPACE_DIR, "LTDOCS.pdf")
|
34 |
with open(pdf_path, 'rb') as file:
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
|
41 |
# Initialize the pipeline
|
42 |
pipe = pipeline(
|
@@ -46,7 +42,7 @@ pipe = pipeline(
|
|
46 |
device="cpu", # replace with "mps" to run on a Mac device
|
47 |
)
|
48 |
|
49 |
-
# System prompt and welcome message
|
50 |
SYSTEM_PROMPT = f"""You Foton the chat bot assistant of the Company Lugha taussi, an AI language assistant specialized in African languages, with a focus on Swahili. Your primary tasks are:
|
51 |
1. Providing accurate translations between Swahili and other languages
|
52 |
2. Teaching Swahili vocabulary and grammar
|
@@ -59,10 +55,39 @@ Always maintain a friendly and patient demeanor, and provide cultural context wh
|
|
59 |
|
60 |
WELCOME_MESSAGE = "**Karibu Lugha Tausi!** Mimi ni Foton, msaidizi wako wa kibinafsi wa Kiswahili. Niko hapa kukusaidia kujifunza, kuelewa, na kuzungumza Kiswahili. **Ninaweza kukusaidiaje leo?** Hebu tuanze! π"
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
def format_chat_message(messages, system_prompt=SYSTEM_PROMPT):
|
63 |
"""Format the chat messages with system prompt"""
|
64 |
formatted_prompt = f"{system_prompt}\n\n"
|
65 |
-
|
66 |
for message in messages:
|
67 |
if isinstance(message, tuple):
|
68 |
role, content = message
|
@@ -70,12 +95,11 @@ def format_chat_message(messages, system_prompt=SYSTEM_PROMPT):
|
|
70 |
formatted_prompt += f"User: {content}\nLugha Tausi: "
|
71 |
elif role == "assistant":
|
72 |
formatted_prompt += f"{content}\n"
|
73 |
-
|
74 |
return formatted_prompt
|
75 |
|
76 |
def chat_response(message, history):
|
77 |
"""Generate response for Gradio chat interface"""
|
78 |
-
# Convert history to the format expected by the model
|
79 |
messages = []
|
80 |
for user_msg, bot_msg in history:
|
81 |
messages.append(("user", user_msg))
|
@@ -93,18 +117,35 @@ def chat_response(message, history):
|
|
93 |
)
|
94 |
return outputs[0]["generated_text"].strip()
|
95 |
|
96 |
-
# Create Gradio interface
|
97 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
def user_input(message, history):
|
110 |
return "", history + [(message, None)]
|
@@ -143,5 +184,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
143 |
|
144 |
# Launch the interface
|
145 |
if __name__ == "__main__":
|
146 |
-
demo.launch(share=True,ssr_mode=False
|
147 |
-
|
|
|
8 |
# Space configuration
|
9 |
SPACE_DIR = os.environ.get("HF_HOME", os.getcwd())
|
10 |
|
|
|
11 |
def init_huggingface_auth():
|
12 |
"""Space-friendly authentication"""
|
13 |
token = os.getenv("HUGGINGFACE_TOKEN")
|
|
|
27 |
if not init_huggingface_auth():
|
28 |
print("Warning: Authentication failed")
|
29 |
|
|
|
30 |
# Load and preprocess the PDF content
|
31 |
pdf_path = os.path.join(SPACE_DIR, "LTDOCS.pdf")
|
32 |
with open(pdf_path, 'rb') as file:
|
33 |
+
pdf_reader = PyPDF2.PdfReader(file)
|
34 |
+
pdf_content = ' '.join([page.extract_text() for page in pdf_reader.pages])
|
35 |
+
pdf_content = pdf_content.lower().strip()
|
|
|
|
|
36 |
|
37 |
# Initialize the pipeline
|
38 |
pipe = pipeline(
|
|
|
42 |
device="cpu", # replace with "mps" to run on a Mac device
|
43 |
)
|
44 |
|
45 |
+
# System prompt and welcome message
|
46 |
SYSTEM_PROMPT = f"""You Foton the chat bot assistant of the Company Lugha taussi, an AI language assistant specialized in African languages, with a focus on Swahili. Your primary tasks are:
|
47 |
1. Providing accurate translations between Swahili and other languages
|
48 |
2. Teaching Swahili vocabulary and grammar
|
|
|
55 |
|
56 |
WELCOME_MESSAGE = "**Karibu Lugha Tausi!** Mimi ni Foton, msaidizi wako wa kibinafsi wa Kiswahili. Niko hapa kukusaidia kujifunza, kuelewa, na kuzungumza Kiswahili. **Ninaweza kukusaidiaje leo?** Hebu tuanze! π"
|
57 |
|
58 |
+
# CSS for custom styling
|
59 |
+
CUSTOM_CSS = """
|
60 |
+
.container {
|
61 |
+
max-width: 800px;
|
62 |
+
margin: auto;
|
63 |
+
padding: 20px;
|
64 |
+
}
|
65 |
+
.header {
|
66 |
+
text-align: center;
|
67 |
+
margin-bottom: 30px;
|
68 |
+
}
|
69 |
+
.icon {
|
70 |
+
width: 80px;
|
71 |
+
height: 80px;
|
72 |
+
margin: 0 auto 15px;
|
73 |
+
display: block;
|
74 |
+
}
|
75 |
+
.title {
|
76 |
+
font-size: 2.5em;
|
77 |
+
font-weight: bold;
|
78 |
+
margin-bottom: 10px;
|
79 |
+
}
|
80 |
+
.description {
|
81 |
+
font-size: 1.2em;
|
82 |
+
color: #666;
|
83 |
+
margin-bottom: 20px;
|
84 |
+
}
|
85 |
+
"""
|
86 |
+
|
87 |
def format_chat_message(messages, system_prompt=SYSTEM_PROMPT):
|
88 |
"""Format the chat messages with system prompt"""
|
89 |
formatted_prompt = f"{system_prompt}\n\n"
|
90 |
+
|
91 |
for message in messages:
|
92 |
if isinstance(message, tuple):
|
93 |
role, content = message
|
|
|
95 |
formatted_prompt += f"User: {content}\nLugha Tausi: "
|
96 |
elif role == "assistant":
|
97 |
formatted_prompt += f"{content}\n"
|
98 |
+
|
99 |
return formatted_prompt
|
100 |
|
101 |
def chat_response(message, history):
|
102 |
"""Generate response for Gradio chat interface"""
|
|
|
103 |
messages = []
|
104 |
for user_msg, bot_msg in history:
|
105 |
messages.append(("user", user_msg))
|
|
|
117 |
)
|
118 |
return outputs[0]["generated_text"].strip()
|
119 |
|
120 |
+
# Create Gradio interface with custom theme and styling
|
121 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
122 |
+
with gr.Column(elem_classes="container"):
|
123 |
+
# Header section with icon, title and description
|
124 |
+
with gr.Column(elem_classes="header"):
|
125 |
+
gr.Image("/api/placeholder/80/80", elem_classes="icon", label="Foton Icon")
|
126 |
+
gr.Markdown("# Foton - Lugha Tausi Assistant", elem_classes="title")
|
127 |
+
gr.Markdown(
|
128 |
+
"""Your personal Swahili language assistant, powered by AI.
|
129 |
+
Specialized in translations, teaching, and cultural context.
|
130 |
+
Let's explore the beauty of Swahili together! πβ¨""",
|
131 |
+
elem_classes="description"
|
132 |
+
)
|
133 |
+
|
134 |
+
# Chat interface
|
135 |
+
chatbot = gr.Chatbot(
|
136 |
+
value=[(None, WELCOME_MESSAGE)],
|
137 |
+
height=500,
|
138 |
+
show_label=False,
|
139 |
+
elem_classes="chatbox"
|
140 |
+
)
|
141 |
+
|
142 |
+
with gr.Row():
|
143 |
+
msg = gr.Textbox(
|
144 |
+
placeholder="Type your message here...",
|
145 |
+
show_label=False,
|
146 |
+
scale=9
|
147 |
+
)
|
148 |
+
clear = gr.Button("Clear Chat", scale=1)
|
149 |
|
150 |
def user_input(message, history):
|
151 |
return "", history + [(message, None)]
|
|
|
184 |
|
185 |
# Launch the interface
|
186 |
if __name__ == "__main__":
|
187 |
+
demo.launch(share=True, ssr_mode=False)
|
|