FESG1234 commited on
Commit
4ee8972
·
verified ·
1 Parent(s): 0c3cf4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -104
app.py CHANGED
@@ -24,131 +24,126 @@ def init_hf_auth():
24
 
25
  init_hf_auth()
26
 
27
- # Model setup
28
 
29
 
 
 
 
 
 
 
 
 
 
 
30
  def load_pdf_content():
31
  try:
32
- import PyPDF2
33
- pdf_path = os.path.join(os.path.dirname(__file__), "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
- return pdf_content.lower().strip()
38
  except Exception as e:
39
- print(f"Error loading PDF: {e}")
40
- return "PDF content not available"
 
 
 
 
 
 
41
 
42
- # Initialize the pipeline
43
- def init_pipeline():
44
  return pipeline(
45
  "text-generation",
46
- model="google/gemma-2-2b-jpn-it",
47
- model_kwargs={"torch_dtype": torch.bfloat16},
48
- device="cpu",
 
 
 
 
49
  )
50
 
51
- # Load PDF content
52
- PDF_CONTENT = load_pdf_content()
53
-
54
- # System prompt and welcome message
55
- 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:
56
- 1. Providing accurate translations between Swahili and other languages
57
- 2. Teaching Swahili vocabulary and grammar
58
- 3. Explaining cultural context behind Swahili expressions
59
- 4. Helping users practice Swahili conversation
60
- 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
61
-
62
- Always maintain a friendly and patient demeanor, and provide cultural context when relevant speak mostly swahili and change when asked.
63
- """
64
-
65
- 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! 😊"
66
 
67
- # Initialize the pipeline globally
68
- pipe = init_pipeline()
 
 
 
 
69
 
70
- def format_chat_message(messages, system_prompt=SYSTEM_PROMPT):
71
- """Format the chat messages with system prompt"""
72
- formatted_prompt = f"{system_prompt}\n\n"
73
 
74
- for message in messages:
75
- if isinstance(message, tuple):
76
- role, content = message
77
- if role == "user":
78
- formatted_prompt += f"User: {content}\nLugha Tausi: "
79
- elif role == "assistant":
80
- formatted_prompt += f"{content}\n"
81
 
82
- return formatted_prompt
83
-
84
- def chat_response(message, history):
85
- """Generate response for Gradio chat interface"""
86
- messages = []
87
- for user_msg, bot_msg in history:
88
- messages.append(("user", user_msg))
89
- messages.append(("assistant", bot_msg))
90
- messages.append(("user", message))
91
-
92
- formatted_input = format_chat_message(messages)
93
- outputs = pipe(
94
- formatted_input,
95
- return_full_text=False,
96
- max_new_tokens=256,
97
- temperature=0.1,
98
- top_p=0.9,
99
- do_sample=True
100
- )
101
- return outputs[0]["generated_text"].strip()
102
-
103
- def user_input(message, history):
104
- return "", history + [(message, None)]
105
-
106
- def bot_response(history):
107
- if len(history) == 0:
108
- history.append((None, WELCOME_MESSAGE))
109
- return history
110
-
111
- user_message = history[-1][0]
112
- bot_message = chat_response(user_message, history[:-1])
113
- history[-1] = (user_message, bot_message)
114
- return history
115
-
116
- def clear_chat():
117
- return [], [(None, WELCOME_MESSAGE)]
118
 
119
- # Create Gradio interface
120
- demo = gr.Blocks(theme=gr.themes.Soft())
 
 
 
121
 
122
- with demo:
 
 
 
 
 
 
123
  chatbot = gr.Chatbot(
124
- value=[(None, WELCOME_MESSAGE)],
125
- height=600,
126
- show_label=False
127
- )
128
- msg = gr.Textbox(
129
- placeholder="Type your message here...",
130
- show_label=False
131
  )
132
- clear = gr.Button("Clear Chat")
133
-
134
- msg.submit(
135
- user_input,
136
- [msg, chatbot],
137
- [msg, chatbot],
138
- queue=False
139
- ).then(
140
- bot_response,
141
- chatbot,
142
- chatbot
 
 
 
 
 
143
  )
144
 
145
- clear.click(
146
- clear_chat,
147
- None,
148
- [chatbot],
149
- queue=False
150
- )
151
 
 
152
  if __name__ == "__main__":
153
- demo.launch()
 
 
 
 
 
154
 
 
24
 
25
  init_hf_auth()
26
 
 
27
 
28
 
29
+ # Configuration
30
+ MODEL_NAME = "google/gemma-2-2b-jpn-it"
31
+ PDF_PATH = "LTDOCS.pdf"
32
+ MAX_TOKENS = 512
33
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
34
+
35
+ # System prompt template
36
+ SYSTEM_PROMPT = """You are Foton...""" # Keep your original prompt
37
+
38
+ # Load PDF content with error handling
39
  def load_pdf_content():
40
  try:
41
+ with open(PDF_PATH, 'rb') as file:
 
 
42
  pdf_reader = PyPDF2.PdfReader(file)
43
+ content = ' '.join([page.extract_text() for page in pdf_reader.pages])
44
+ return content.lower()
45
  except Exception as e:
46
+ print(f"PDF Error: {str(e)}")
47
+ return ""
48
+
49
+ pdf_content = load_pdf_content()
50
+
51
+ # Initialize model with progress tracking
52
+ with gr.Blocks() as loading_screen:
53
+ gr.Markdown("⏳ Inapakia mfano wa Kiswahili...")
54
 
55
+ def create_pipeline():
 
56
  return pipeline(
57
  "text-generation",
58
+ model=MODEL_NAME,
59
+ device=DEVICE,
60
+ model_kwargs={
61
+ "torch_dtype": torch.bfloat16,
62
+ "token": os.environ.get("HF_TOKEN"),
63
+ "cache_dir": "model_cache"
64
+ }
65
  )
66
 
67
+ try:
68
+ pipe = create_pipeline()
69
+ except Exception as e:
70
+ raise gr.Error(f"Failed to load model: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
71
 
72
+ # Chat processing
73
+ def format_prompt(history):
74
+ full_prompt = SYSTEM_PROMPT.format(pdf_content=pdf_content)
75
+ for user_msg, bot_msg in history:
76
+ full_prompt += f"\nUser: {user_msg}\nAssistant: {bot_msg or ''}"
77
+ return full_prompt
78
 
79
+ def respond(message, history):
80
+ if not message.strip():
81
+ raise gr.Error("Tafadhali ingiza ujumbe halisi")
82
 
83
+ prompt = format_prompt(history + [[message, ""]])
 
 
 
 
 
 
84
 
85
+ try:
86
+ response = pipe(
87
+ prompt,
88
+ max_new_tokens=MAX_TOKENS,
89
+ temperature=0.7,
90
+ top_p=0.9,
91
+ do_sample=True,
92
+ truncation=True
93
+ )[0]['generated_text']
94
+
95
+ # Extract only the new response
96
+ return response[len(prompt):].split("User:")[0].strip()
97
+
98
+ except Exception as e:
99
+ print(f"Generation Error: {str(e)}")
100
+ raise gr.Error("Nimeshindwa kuzaliza majibu. Tafadhali jaribu tena baadaye.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
+ # Interface design
103
+ theme = gr.themes.Soft(
104
+ primary_hue="emerald",
105
+ secondary_hue="teal"
106
+ )
107
 
108
+ with gr.Blocks(theme=theme, title="Lugha Tausi") as demo:
109
+ gr.Markdown("# 🇹🇿 Foton - Msaidizi Wa Kiswahili")
110
+
111
+ with gr.Row():
112
+ gr.Image("logo.png", width=100, show_label=False)
113
+ gr.Markdown("### Karibu kwa Huduma ya Lugha Tausi!")
114
+
115
  chatbot = gr.Chatbot(
116
+ value=[(None, "Karibu! Nipo kukusaidia na Kiswahili.")],
117
+ bubble_full_width=False,
118
+ avatar_images=("user.jpg", "bot.jpg")
 
 
 
 
119
  )
120
+
121
+ with gr.Row():
122
+ msg = gr.Textbox(
123
+ placeholder="Andika hapa...",
124
+ max_lines=3,
125
+ autofocus=True
126
+ )
127
+ submit = gr.Button("Tuma", variant="primary")
128
+
129
+ examples = gr.Examples(
130
+ examples=[
131
+ ["Nisaidie kutafsiri sentensi hii"],
132
+ ["Ninauliza kuhusu sarufi ya Kiswahili"],
133
+ ["Eleza maana ya 'Haraka haraka haina baraka'"]
134
+ ],
135
+ inputs=msg
136
  )
137
 
138
+ submit.click(respond, [msg, chatbot], [msg, chatbot])
139
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
 
 
 
 
140
 
141
+ # Deployment settings
142
  if __name__ == "__main__":
143
+ demo.launch(
144
+ server_name="0.0.0.0",
145
+ server_port=7860,
146
+
147
+ auth=os.environ.get("SPACE_AUTH")
148
+ )
149