SHAILJA1 commited on
Commit
fa6963d
·
verified ·
1 Parent(s): 4278377

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
+
4
+ # Load a public, open-access model
5
+ model_name = "tiiuae/falcon-7b-instruct"
6
+
7
+ # Load tokenizer and model
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForCausalLM.from_pretrained(
10
+ model_name,
11
+ device_map="auto",
12
+ trust_remote_code=True
13
+ )
14
+
15
+ # Create text-generation pipeline
16
+ chat_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=200)
17
+
18
+ # Chat function
19
+ def chat(user_input, history=[]):
20
+ prompt = f"User: {user_input}\nAssistant:"
21
+ result = chat_pipeline(prompt)[0]["generated_text"]
22
+ response = result.split("Assistant:")[-1].strip()
23
+ history.append((user_input, response))
24
+ return history, history
25
+
26
+ # Gradio UI
27
+ interface = gr.ChatInterface(
28
+ fn=chat,
29
+ chatbot=gr.Chatbot(),
30
+ textbox=gr.Textbox(placeholder="Ask a question..."),
31
+ title="🗣️ Falcon 7B Chatbot",
32
+ description="This chatbot uses the Falcon 7B Instruct model from Hugging Face.",
33
+ )
34
+
35
+ interface.launch()