Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,12 +3,20 @@ import torch
|
|
3 |
from transformers import pipeline
|
4 |
import gradio as gr
|
5 |
|
|
|
|
|
6 |
import os
|
7 |
|
8 |
|
9 |
from huggingface_hub import login, HfFolder
|
10 |
from getpass import getpass
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def init_huggingface_auth():
|
13 |
"""
|
14 |
Initialize Hugging Face authentication with multiple fallback options
|
@@ -51,67 +59,110 @@ if not init_huggingface_auth():
|
|
51 |
|
52 |
print("Authentication successful - continuing with the application")
|
53 |
|
|
|
54 |
# Initialize the pipeline
|
55 |
pipe = pipeline(
|
56 |
"text-generation",
|
57 |
model="google/gemma-2-2b-jpn-it",
|
58 |
-
|
59 |
device="cpu", # replace with "mps" to run on a Mac device
|
60 |
)
|
61 |
|
62 |
-
#
|
63 |
-
SYSTEM_PROMPT = """You
|
64 |
1. Providing accurate translations between Swahili and other languages
|
65 |
2. Teaching Swahili vocabulary and grammar
|
66 |
3. Explaining cultural context behind Swahili expressions
|
67 |
4. Helping users practice Swahili conversation
|
|
|
68 |
|
69 |
-
Always maintain a friendly and patient demeanor, and provide cultural context when relevant speak mostly
|
70 |
"""
|
71 |
|
72 |
-
WELCOME_MESSAGE = "**
|
73 |
|
74 |
def format_chat_message(messages, system_prompt=SYSTEM_PROMPT):
|
75 |
"""Format the chat messages with system prompt"""
|
76 |
formatted_prompt = f"{system_prompt}\n\n"
|
77 |
-
|
78 |
-
# Add welcome message if this is the first interaction
|
79 |
-
if not messages:
|
80 |
-
formatted_prompt += f"Lugha Tausi: {WELCOME_MESSAGE}\n"
|
81 |
-
|
82 |
for message in messages:
|
83 |
-
if message
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
88 |
return formatted_prompt
|
89 |
|
90 |
-
def
|
91 |
-
"""Generate response
|
92 |
-
#
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
96 |
formatted_input = format_chat_message(messages)
|
97 |
outputs = pipe(
|
98 |
formatted_input,
|
99 |
return_full_text=False,
|
100 |
max_new_tokens=256,
|
101 |
-
temperature=0.1,
|
102 |
top_p=0.9,
|
103 |
do_sample=True
|
104 |
)
|
105 |
return outputs[0]["generated_text"].strip()
|
106 |
|
107 |
-
#
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
|
|
116 |
if __name__ == "__main__":
|
117 |
demo.launch(share=True,pwa=True )
|
|
|
3 |
from transformers import pipeline
|
4 |
import gradio as gr
|
5 |
|
6 |
+
import PyPDF2
|
7 |
+
|
8 |
import os
|
9 |
|
10 |
|
11 |
from huggingface_hub import login, HfFolder
|
12 |
from getpass import getpass
|
13 |
|
14 |
+
# Load and preprocess the PDF content
|
15 |
+
with open('/content/LTDOCS.pdf', 'rb') as file:
|
16 |
+
pdf_reader = PyPDF2.PdfReader(file)
|
17 |
+
pdf_content = ' '.join([page.extract_text() for page in pdf_reader.pages])
|
18 |
+
pdf_content = pdf_content.lower().strip()
|
19 |
+
|
20 |
def init_huggingface_auth():
|
21 |
"""
|
22 |
Initialize Hugging Face authentication with multiple fallback options
|
|
|
59 |
|
60 |
print("Authentication successful - continuing with the application")
|
61 |
|
62 |
+
|
63 |
# Initialize the pipeline
|
64 |
pipe = pipeline(
|
65 |
"text-generation",
|
66 |
model="google/gemma-2-2b-jpn-it",
|
67 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
68 |
device="cpu", # replace with "mps" to run on a Mac device
|
69 |
)
|
70 |
|
71 |
+
# System prompt and welcome message (using your existing definitions)
|
72 |
+
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:
|
73 |
1. Providing accurate translations between Swahili and other languages
|
74 |
2. Teaching Swahili vocabulary and grammar
|
75 |
3. Explaining cultural context behind Swahili expressions
|
76 |
4. Helping users practice Swahili conversation
|
77 |
+
5. Based on the programing doc for lughah Tausi Programing which is in swahili , the following information is relevant: {pdf_content} .assist users in programing and installing lugha tausi programing language"
|
78 |
|
79 |
+
Always maintain a friendly and patient demeanor, and provide cultural context when relevant speak mostly swahili and change when asked.
|
80 |
"""
|
81 |
|
82 |
+
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! 😊"
|
83 |
|
84 |
def format_chat_message(messages, system_prompt=SYSTEM_PROMPT):
|
85 |
"""Format the chat messages with system prompt"""
|
86 |
formatted_prompt = f"{system_prompt}\n\n"
|
87 |
+
|
|
|
|
|
|
|
|
|
88 |
for message in messages:
|
89 |
+
if isinstance(message, tuple):
|
90 |
+
role, content = message
|
91 |
+
if role == "user":
|
92 |
+
formatted_prompt += f"User: {content}\nLugha Tausi: "
|
93 |
+
elif role == "assistant":
|
94 |
+
formatted_prompt += f"{content}\n"
|
95 |
+
|
96 |
return formatted_prompt
|
97 |
|
98 |
+
def chat_response(message, history):
|
99 |
+
"""Generate response for Gradio chat interface"""
|
100 |
+
# Convert history to the format expected by the model
|
101 |
+
messages = []
|
102 |
+
for user_msg, bot_msg in history:
|
103 |
+
messages.append(("user", user_msg))
|
104 |
+
messages.append(("assistant", bot_msg))
|
105 |
+
messages.append(("user", message))
|
106 |
+
|
107 |
formatted_input = format_chat_message(messages)
|
108 |
outputs = pipe(
|
109 |
formatted_input,
|
110 |
return_full_text=False,
|
111 |
max_new_tokens=256,
|
112 |
+
temperature=0.1,
|
113 |
top_p=0.9,
|
114 |
do_sample=True
|
115 |
)
|
116 |
return outputs[0]["generated_text"].strip()
|
117 |
|
118 |
+
# Create Gradio interface
|
119 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
120 |
+
chatbot = gr.Chatbot(
|
121 |
+
value=[(None, WELCOME_MESSAGE)],
|
122 |
+
height=600,
|
123 |
+
show_label=False
|
124 |
+
)
|
125 |
+
msg = gr.Textbox(
|
126 |
+
placeholder="Type your message here...",
|
127 |
+
show_label=False
|
128 |
+
)
|
129 |
+
clear = gr.Button("Clear Chat")
|
130 |
+
|
131 |
+
def user_input(message, history):
|
132 |
+
return "", history + [(message, None)]
|
133 |
+
|
134 |
+
def bot_response(history):
|
135 |
+
if len(history) == 0:
|
136 |
+
history.append((None, WELCOME_MESSAGE))
|
137 |
+
return history
|
138 |
|
139 |
+
user_message = history[-1][0]
|
140 |
+
bot_message = chat_response(user_message, history[:-1])
|
141 |
+
history[-1] = (user_message, bot_message)
|
142 |
+
return history
|
143 |
+
|
144 |
+
def clear_chat():
|
145 |
+
return [], [(None, WELCOME_MESSAGE)]
|
146 |
+
|
147 |
+
# Set up the message flow
|
148 |
+
msg.submit(
|
149 |
+
user_input,
|
150 |
+
[msg, chatbot],
|
151 |
+
[msg, chatbot],
|
152 |
+
queue=False
|
153 |
+
).then(
|
154 |
+
bot_response,
|
155 |
+
chatbot,
|
156 |
+
chatbot
|
157 |
+
)
|
158 |
+
|
159 |
+
clear.click(
|
160 |
+
clear_chat,
|
161 |
+
None,
|
162 |
+
[chatbot],
|
163 |
+
queue=False
|
164 |
+
)
|
165 |
|
166 |
+
# Launch the interface
|
167 |
if __name__ == "__main__":
|
168 |
demo.launch(share=True,pwa=True )
|