Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,40 +6,53 @@ import os
|
|
6 |
from huggingface_hub import login, HfFolder
|
7 |
from getpass import getpass
|
8 |
|
|
|
|
|
|
|
9 |
# Load and preprocess the PDF content
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
def init_huggingface_auth():
|
16 |
-
"""
|
17 |
-
|
18 |
-
print("Already logged in to Hugging Face!")
|
19 |
-
return True
|
20 |
|
21 |
-
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
return False
|
31 |
-
return False
|
32 |
|
33 |
if not init_huggingface_auth():
|
34 |
print("Warning: Authentication failed")
|
35 |
|
36 |
-
# Initialize the pipeline
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
SYSTEM_PROMPT = f"""You are Foton, Swahili AI assistant. Tasks:
|
45 |
1. Swahili translations
|
@@ -67,22 +80,26 @@ def format_messages(history):
|
|
67 |
|
68 |
def generate_response(message, history):
|
69 |
"""Generate AI response"""
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
# Create Gradio interface with
|
86 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
87 |
gr.Markdown("# Lugha Tausi - Swahili Assistant")
|
88 |
|
@@ -90,9 +107,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
90 |
value=[[None, WELCOME_MESSAGE]],
|
91 |
height=600,
|
92 |
show_label=False,
|
93 |
-
avatar_images=(
|
94 |
-
|
95 |
-
|
|
|
96 |
)
|
97 |
|
98 |
msg = gr.Textbox(placeholder="Andika ujumbe wako hapa...", show_label=False)
|
@@ -108,10 +126,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
108 |
|
109 |
if __name__ == "__main__":
|
110 |
demo.launch(
|
111 |
-
inline=False, # Required for Spaces
|
112 |
-
share=True, # Required for Spaces
|
113 |
-
debug=True,
|
114 |
server_name="0.0.0.0",
|
115 |
server_port=7860,
|
|
|
116 |
show_error=True
|
117 |
)
|
|
|
6 |
from huggingface_hub import login, HfFolder
|
7 |
from getpass import getpass
|
8 |
|
9 |
+
# Space configuration
|
10 |
+
SPACE_DIR = os.environ.get("HF_HOME", os.getcwd())
|
11 |
+
|
12 |
# Load and preprocess the PDF content
|
13 |
+
try:
|
14 |
+
pdf_path = os.path.join(SPACE_DIR, "LTDOCS.pdf")
|
15 |
+
with open(pdf_path, '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 |
+
except Exception as e:
|
20 |
+
pdf_content = ""
|
21 |
+
print(f"Error loading PDF: {e}")
|
22 |
|
23 |
def init_huggingface_auth():
|
24 |
+
"""Space-friendly authentication"""
|
25 |
+
token = os.getenv("HUGGINGFACE_TOKEN")
|
|
|
|
|
26 |
|
27 |
+
if not token:
|
28 |
+
print("No HF token found in environment")
|
29 |
+
return False
|
30 |
|
31 |
+
try:
|
32 |
+
login(token=token, add_to_git_credential=False)
|
33 |
+
print("HF authentication successful")
|
34 |
+
return True
|
35 |
+
except Exception as e:
|
36 |
+
print(f"Login error: {e}")
|
37 |
+
return False
|
|
|
|
|
38 |
|
39 |
if not init_huggingface_auth():
|
40 |
print("Warning: Authentication failed")
|
41 |
|
42 |
+
# Initialize the pipeline with CPU optimization
|
43 |
+
try:
|
44 |
+
pipe = pipeline(
|
45 |
+
"text-generation",
|
46 |
+
model="google/gemma-2-2b-jpn-it",
|
47 |
+
device_map="auto",
|
48 |
+
model_kwargs={
|
49 |
+
"torch_dtype": torch.float16,
|
50 |
+
"low_cpu_mem_usage": True
|
51 |
+
}
|
52 |
+
)
|
53 |
+
except Exception as e:
|
54 |
+
print(f"Model loading error: {e}")
|
55 |
+
raise
|
56 |
|
57 |
SYSTEM_PROMPT = f"""You are Foton, Swahili AI assistant. Tasks:
|
58 |
1. Swahili translations
|
|
|
80 |
|
81 |
def generate_response(message, history):
|
82 |
"""Generate AI response"""
|
83 |
+
try:
|
84 |
+
formatted_history = format_messages(history)
|
85 |
+
formatted_history.append({"role": "user", "content": message})
|
86 |
+
|
87 |
+
prompt = "\n".join([f"{m['role']}: {m['content']}" for m in formatted_history])
|
88 |
+
|
89 |
+
output = pipe(
|
90 |
+
prompt,
|
91 |
+
max_new_tokens=256,
|
92 |
+
temperature=0.1,
|
93 |
+
top_p=0.9,
|
94 |
+
do_sample=True,
|
95 |
+
return_full_text=False
|
96 |
+
)
|
97 |
+
return output[0]["generated_text"].strip()
|
98 |
+
except Exception as e:
|
99 |
+
print(f"Generation error: {e}")
|
100 |
+
return "Samahani, nimekutana na tatizo. Tafadhali jaribu tena baadaye."
|
101 |
|
102 |
+
# Create Gradio interface with Space optimizations
|
103 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
104 |
gr.Markdown("# Lugha Tausi - Swahili Assistant")
|
105 |
|
|
|
107 |
value=[[None, WELCOME_MESSAGE]],
|
108 |
height=600,
|
109 |
show_label=False,
|
110 |
+
avatar_images=(None, "user.png"),
|
111 |
+
bubble_full_width=False,
|
112 |
+
show_share_button=False,
|
113 |
+
type="messages"
|
114 |
)
|
115 |
|
116 |
msg = gr.Textbox(placeholder="Andika ujumbe wako hapa...", show_label=False)
|
|
|
126 |
|
127 |
if __name__ == "__main__":
|
128 |
demo.launch(
|
|
|
|
|
|
|
129 |
server_name="0.0.0.0",
|
130 |
server_port=7860,
|
131 |
+
auth=os.getenv("SPACE_AUTH"),
|
132 |
show_error=True
|
133 |
)
|