Adityabhatia0204 commited on
Commit
3eb62cf
·
verified ·
1 Parent(s): 7ad820a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -1,29 +1,31 @@
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
 
4
- # Load model and tokenizer (it will automatically pull from Hugging Face hub)
5
  model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
6
 
7
  tokenizer = AutoTokenizer.from_pretrained(model_id)
8
  model = AutoModelForCausalLM.from_pretrained(model_id)
9
 
10
- # Create text-generation pipeline
11
  generator = pipeline(
12
- "text-generation",
13
- model=model,
14
- tokenizer=tokenizer,
15
  max_new_tokens=200,
16
  temperature=0.7,
17
  top_p=0.9
18
  )
19
 
20
- # Define chatbot function
21
- def chat(prompt):
22
- response = generator(prompt)
23
- return response[0]['generated_text']
24
 
25
- # Build Gradio interface
26
- iface = gr.Interface(fn=chat, inputs="text", outputs="text", title="Self-Hosted AI Chatbot (CPU)")
 
 
 
 
 
27
 
28
- # Launch app
29
  iface.launch()
 
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
 
 
4
  model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
5
 
6
  tokenizer = AutoTokenizer.from_pretrained(model_id)
7
  model = AutoModelForCausalLM.from_pretrained(model_id)
8
 
 
9
  generator = pipeline(
10
+ "text-generation",
11
+ model=model,
12
+ tokenizer=tokenizer,
13
  max_new_tokens=200,
14
  temperature=0.7,
15
  top_p=0.9
16
  )
17
 
18
+ def chat(user_input):
19
+ prompt = f"You are a helpful assistant.\nUser: {user_input}\nAssistant:"
20
+ response = generator(prompt, return_full_text=False)
21
+ return response[0]['generated_text'].strip()
22
 
23
+ iface = gr.Interface(
24
+ fn=chat,
25
+ inputs=gr.Textbox(lines=2, placeholder="Ask a full question, like 'Where is Delhi?'"),
26
+ outputs="text",
27
+ title="TinyLlama Chatbot 🤖",
28
+ description="Lightweight chatbot powered by TinyLlama. Works better with complete questions!"
29
+ )
30
 
 
31
  iface.launch()