FESG1234 commited on
Commit
c267ccd
·
verified ·
1 Parent(s): c6fd6f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -86
app.py CHANGED
@@ -8,16 +8,6 @@ from huggingface_hub import login
8
  # Space configuration
9
  SPACE_DIR = os.environ.get("HF_HOME", os.getcwd())
10
 
11
- # Load and preprocess the PDF content
12
- try:
13
- pdf_path = os.path.join(SPACE_DIR, "LTDOCS.pdf")
14
- with open(pdf_path, 'rb') as file:
15
- pdf_reader = PyPDF2.PdfReader(file)
16
- pdf_content = ' '.join([page.extract_text() for page in pdf_reader.pages])
17
- pdf_content = pdf_content.lower().strip()
18
- except Exception as e:
19
- pdf_content = ""
20
- print(f"Error loading PDF: {e}")
21
 
22
  def init_huggingface_auth():
23
  """Space-friendly authentication"""
@@ -38,91 +28,120 @@ def init_huggingface_auth():
38
  if not init_huggingface_auth():
39
  print("Warning: Authentication failed")
40
 
41
- # Initialize the pipeline with CPU optimization
42
- try:
43
- pipe = pipeline(
44
- "text-generation",
45
- model="google/gemma-2-2b-jpn-it",
46
- device_map="auto",
47
- model_kwargs={
48
- "torch_dtype": torch.float16,
49
- "low_cpu_mem_usage": True
50
- }
51
- )
52
- except Exception as e:
53
- print(f"Model loading error: {e}")
54
- raise
55
-
56
- SYSTEM_PROMPT = f"""You are Foton, Swahili AI assistant. Tasks:
57
- 1. Swahili translations
58
- 2. Teach Swahili vocabulary/grammar
59
- 3. Explain cultural context
60
- 4. Help practice conversations
61
- 5. Programming assistance using: {pdf_content}
62
-
63
- Maintain friendly, patient demeanor with cultural context.
 
 
 
 
64
  """
65
 
66
- def format_messages(history):
67
- """Format chat history with system prompt"""
68
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
69
- for msg in history:
70
- if isinstance(msg, dict) and "role" in msg and "content" in msg:
71
- messages.append(msg)
72
- return messages
73
 
74
- def generate_response(history):
75
- """Generate AI response"""
76
- try:
77
- messages = format_messages(history)
78
-
79
- prompt = "\n".join([f"{m['role']}: {m['content']}" for m in messages])
80
-
81
- output = pipe(
82
- prompt,
83
- max_new_tokens=256,
84
- temperature=0.1,
85
- top_p=0.9,
86
- do_sample=True,
87
- return_full_text=False
88
- )
89
- return output[0]["generated_text"].strip()
90
- except Exception as e:
91
- print(f"Generation error: {e}")
92
- return "Samahani, nimekutana na tatizo. Tafadhali jaribu tena baadaye."
93
 
94
- # Create Gradio interface with Space optimizations
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
96
- gr.Markdown("# Lugha Tausi - Swahili Assistant")
97
-
98
  chatbot = gr.Chatbot(
99
- value=[{"role": "assistant", "content": "**Karibu Lugha Tausi!** Mimi ni Foton, msaidizi wako wa Kiswahili. Niko hapa kukusaidia kujifunza na kuzungumza Kiswahili. **Ninaweza kukusaidiaje leo?** 😊"}],
100
  height=600,
101
- show_label=False,
102
- avatar_images=(None, "user.png"),
103
- bubble_full_width=False,
104
- show_share_button=False,
105
- type="messages"
106
  )
107
-
108
- msg = gr.Textbox(placeholder="Andika ujumbe wako hapa...", show_label=False)
109
- clear = gr.Button("Futa Mazungumzo")
 
 
110
 
111
- def respond(message, history):
112
- history.append({"role": "user", "content": message})
113
- bot_response = generate_response(history)
114
- history.append({"role": "assistant", "content": bot_response})
115
- return "", history
116
 
117
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
118
- clear.click(lambda: [None, []], None, [msg, chatbot], queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
 
120
  if __name__ == "__main__":
121
- demo.launch(
122
- server_name="0.0.0.0",
123
- server_port=7860,
124
- auth=os.getenv("SPACE_AUTH"),
125
- show_error=True,
126
- share=True, # Explicitly disable sharing
127
- ssr_mode=False
128
- )
 
8
  # Space configuration
9
  SPACE_DIR = os.environ.get("HF_HOME", os.getcwd())
10
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  def init_huggingface_auth():
13
  """Space-friendly authentication"""
 
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
+ pdf_reader = PyPDF2.PdfReader(file)
36
+ pdf_content = ' '.join([page.extract_text() for page in pdf_reader.pages])
37
+ pdf_content = pdf_content.lower().strip()
38
+
39
+
40
+
41
+ # Initialize the pipeline
42
+ pipe = pipeline(
43
+ "text-generation",
44
+ model="google/gemma-2-2b-jpn-it",
45
+ model_kwargs={"torch_dtype": torch.bfloat16},
46
+ device="cpu", # replace with "mps" to run on a Mac device
47
+ )
48
+
49
+ # System prompt and welcome message (using your existing definitions)
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
53
+ 3. Explaining cultural context behind Swahili expressions
54
+ 4. Helping users practice Swahili conversation
55
+ 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"
56
+
57
+ Always maintain a friendly and patient demeanor, and provide cultural context when relevant speak mostly swahili and change when asked.
58
  """
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
69
+ if role == "user":
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))
82
+ messages.append(("assistant", bot_msg))
83
+ messages.append(("user", message))
84
+
85
+ formatted_input = format_chat_message(messages)
86
+ outputs = pipe(
87
+ formatted_input,
88
+ return_full_text=False,
89
+ max_new_tokens=256,
90
+ temperature=0.1,
91
+ top_p=0.9,
92
+ do_sample=True
93
+ )
94
+ return outputs[0]["generated_text"].strip()
95
+
96
+ # Create Gradio interface
97
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
 
98
  chatbot = gr.Chatbot(
99
+ value=[(None, WELCOME_MESSAGE)],
100
  height=600,
101
+ show_label=False
 
 
 
 
102
  )
103
+ msg = gr.Textbox(
104
+ placeholder="Type your message here...",
105
+ show_label=False
106
+ )
107
+ clear = gr.Button("Clear Chat")
108
 
109
+ def user_input(message, history):
110
+ return "", history + [(message, None)]
 
 
 
111
 
112
+ def bot_response(history):
113
+ if len(history) == 0:
114
+ history.append((None, WELCOME_MESSAGE))
115
+ return history
116
+
117
+ user_message = history[-1][0]
118
+ bot_message = chat_response(user_message, history[:-1])
119
+ history[-1] = (user_message, bot_message)
120
+ return history
121
+
122
+ def clear_chat():
123
+ return [], [(None, WELCOME_MESSAGE)]
124
+
125
+ # Set up the message flow
126
+ msg.submit(
127
+ user_input,
128
+ [msg, chatbot],
129
+ [msg, chatbot],
130
+ queue=False
131
+ ).then(
132
+ bot_response,
133
+ chatbot,
134
+ chatbot
135
+ )
136
+
137
+ clear.click(
138
+ clear_chat,
139
+ None,
140
+ [chatbot],
141
+ queue=False
142
+ )
143
 
144
+ # Launch the interface
145
  if __name__ == "__main__":
146
+ demo.launch(share=True,ssr_mode=False )
147
+