Bur3hani commited on
Commit
96c09a4
·
verified ·
1 Parent(s): 6b417a7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
+
4
+ # --- 1. Load your Standalone Model from the Hugging Face Hub ---
5
+ # This is the most important change. We now point directly to your repository.
6
+ MODEL_ID = "Bur3hani/karani_ofline"
7
+
8
+ print(f"Loading tokenizer from Hub: {MODEL_ID}...")
9
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
10
+ print("✅ Tokenizer loaded.")
11
+
12
+ print(f"Loading fine-tuned model from Hub: {MODEL_ID}...")
13
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
14
+ print("✅ Model loaded.")
15
+
16
+
17
+ # --- 2. Define the Prediction Function ---
18
+ # This function takes the user's message and chat history to generate a response.
19
+ def get_chat_response(message, history):
20
+ # Format the conversation history for the model.
21
+ input_text = ""
22
+ for user_turn, bot_turn in history:
23
+ # We create a string representing the conversation turns
24
+ input_text += f"user: {user_turn} bot: {bot_turn} "
25
+ # Add the latest user message
26
+ input_text += f"user: {message}"
27
+
28
+ # Tokenize the entire conversation history
29
+ inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
30
+
31
+ # Generate a response from your fine-tuned model
32
+ outputs = model.generate(**inputs, max_length=60, num_beams=4, early_stopping=True)
33
+
34
+ # Decode the response and return it for the chatbot UI
35
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
36
+ return response
37
+
38
+
39
+ # --- 3. Build and Launch the Gradio Chat Interface ---
40
+ demo = gr.ChatInterface(
41
+ fn=get_chat_response,
42
+ title="Karani v1 - AI Secretary (Offline Model)",
43
+ description="A conversational AI assistant for Kiswahili, powered by a custom fine-tuned model. Ask me anything!",
44
+ chatbot=gr.Chatbot(height=500),
45
+ textbox=gr.Textbox(placeholder="Andika ujumbe wako hapa... (Type your message here...)", container=False, scale=7),
46
+ theme="soft",
47
+ examples=[["Habari za asubuhi?"], ["Ni nini mpango wa leo?"], ["Naweza kupata muhtasari wa habari?"]],
48
+ cache_examples=False,
49
+ retry_btn=None,
50
+ undo_btn="Futa (Delete)",
51
+ clear_btn="Futa Mazungumzo (Clear Chat)",
52
+ )
53
+
54
+ if __name__ == "__main__":
55
+ demo.launch()