FESG1234 commited on
Commit
1d5f072
·
verified ·
1 Parent(s): 1919a0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -46
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
- with open('./LTDOCS.pdf', 'rb') as file:
11
- pdf_reader = PyPDF2.PdfReader(file)
12
- pdf_content = ' '.join([page.extract_text() for page in pdf_reader.pages])
13
- pdf_content = pdf_content.lower().strip()
 
 
 
 
 
14
 
15
  def init_huggingface_auth():
16
- """Initialize Hugging Face authentication"""
17
- if HfFolder.get_token():
18
- print("Already logged in to Hugging Face!")
19
- return True
20
 
21
- token = os.getenv('HUGGINGFACE_TOKEN') or getpass("Enter Hugging Face Token: ")
 
 
22
 
23
- if token:
24
- try:
25
- login(token=token, add_to_git_credential=True)
26
- print("Successfully logged in!")
27
- return True
28
- except Exception as e:
29
- print(f"Login error: {e}")
30
- return False
31
- return False
32
 
33
  if not init_huggingface_auth():
34
  print("Warning: Authentication failed")
35
 
36
- # Initialize the pipeline
37
- pipe = pipeline(
38
- "text-generation",
39
- model="google/gemma-2-2b-jpn-it",
40
- model_kwargs={"torch_dtype": torch.bfloat16},
41
- device="cpu",
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
- formatted_history = format_messages(history)
71
- formatted_history.append({"role": "user", "content": message})
72
-
73
- prompt = "\n".join([f"{m['role']}: {m['content']}" for m in formatted_history])
74
-
75
- output = pipe(
76
- prompt,
77
- max_new_tokens=256,
78
- temperature=0.1,
79
- top_p=0.9,
80
- do_sample=True,
81
- return_full_text=False
82
- )
83
- return output[0]["generated_text"].strip()
 
 
 
 
84
 
85
- # Create Gradio interface with corrected settings
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
- None, (os.path.join(os.path.dirname(__file__),"user.png"))
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
  )