itsMaaz commited on
Commit
a2daedd
·
verified ·
1 Parent(s): 2865b56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -51
app.py CHANGED
@@ -1,52 +1,40 @@
1
- import os
2
  import gradio as gr
3
- import torch
4
- from transformers import AutoModelForCausalLM, AutoTokenizer
5
-
6
- # Use your Hugging Face token from the Secrets
7
- HF_TOKEN = os.environ.get("HF_TOKEN")
8
-
9
- # Model (lightweight, public, works on CPU)
10
- MODEL_NAME = "TheBloke/Gemma-3-270M-GGML"
11
-
12
- # Load tokenizer and model
13
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_auth_token=HF_TOKEN)
14
- model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, use_auth_token=HF_TOKEN).to("cpu")
15
-
16
- history = []
17
-
18
- def chat_with_airi(user_msg):
19
- global history
20
- # build simple prompt
21
- prompt = ""
22
- for u, a in history[-5:]:
23
- prompt += f"User: {u}\nAiri: {a}\n"
24
- prompt += f"User: {user_msg}\nAiri:"
25
-
26
- inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
27
- with torch.no_grad():
28
- output = model.generate(
29
- **inputs,
30
- max_new_tokens=60,
31
- do_sample=True,
32
- top_p=0.9,
33
- temperature=0.8,
34
- pad_token_id=tokenizer.eos_token_id
35
- )
36
-
37
- reply = tokenizer.decode(output[0], skip_special_tokens=True)
38
- reply = reply.split("Airi:", 1)[-1].strip()
39
-
40
- history.append([user_msg, reply])
41
- return history, ""
42
-
43
- # Gradio interface
44
- with gr.Blocks() as demo:
45
- gr.HTML("<h2 style='text-align:center'>Airi — Mini Chat AI</h2>")
46
- gr.HTML("<p style='text-align:center;color:#666;'>Small, Fast & Public Model</p>")
47
-
48
- chat = gr.Chatbot()
49
- msg = gr.Textbox(label="Talk to Airi…", placeholder="Write here…")
50
- msg.submit(chat_with_airi, msg, [chat, msg])
51
-
52
- demo.launch()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Initialize the chatbot model (publicly available, no auth needed)
5
+ # Using a lightweight model for fast responses
6
+ model_id = "facebook/blenderbot-400M-distill"
7
+ chatbot = pipeline("conversational", model=model_id)
8
+
9
+ def chat_function(message, history):
10
+ """
11
+ Simple chat function that takes user input and returns AI response
12
+ """
13
+ # Convert history to format expected by the model
14
+ conversation = []
15
+ for human, ai in history:
16
+ conversation.append({"role": "user", "content": human})
17
+ conversation.append({"role": "assistant", "content": ai})
18
+
19
+ conversation.append({"role": "user", "content": message})
20
+
21
+ # Generate response
22
+ response = chatbot(conversation)
23
+ return response[-1]["content"]
24
+
25
+ # Create the chat interface
26
+ demo = gr.ChatInterface(
27
+ fn=chat_function,
28
+ title="airi",
29
+ description="A friendly AI assistant ready to chat with you!",
30
+ theme=gr.themes.Soft(),
31
+ examples=[
32
+ "Hello! How are you?",
33
+ "Tell me something interesting",
34
+ "What's your favorite topic?"
35
+ ],
36
+ cache_examples=False,
37
+ )
38
+
39
+ if __name__ == "__main__":
40
+ demo.launch()