luelhagos commited on
Commit
32a88a7
1 Parent(s): 1846f19

Added Gradio app

Browse files
Files changed (2) hide show
  1. app.py +36 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from peft import AutoPeftModelForCausalLM
3
+ from transformers import AutoTokenizer, pipeline
4
+ import gradio as gr
5
+
6
+
7
+ peft_model_id = "Pr123/TinyLlama-EA-Chat"
8
+
9
+ # Load Model with PEFT adapter
10
+ model = AutoPeftModelForCausalLM.from_pretrained(
11
+ peft_model_id,
12
+ device_map="auto",
13
+ torch_dtype=torch.float16
14
+ )
15
+ tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
16
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=500)
17
+
18
+
19
+ def chat_with_tinyllm(prompt):
20
+ instruction = "Answer the following question: if you don't know the answer, just say that you don't know; don't try to make up an answer."
21
+ prompt_content = f"<s>[INST] <<SYS>>{instruction}<</SYS>>{prompt}[/INST]"
22
+ result = pipe(prompt_content)
23
+ result = result[0]['generated_text'].split('[/INST]')[-1]
24
+ return result
25
+
26
+ def chat_interface():
27
+ iface = gr.Interface(
28
+ fn=chat_with_tinyllm,
29
+ inputs=gr.inputs.Textbox(lines=2, placeholder="Type your question here..."),
30
+ outputs="text",
31
+ title="Chat with TinyLlama",
32
+ description="This is a simple chatbot powered by a fine-tuned model on Hugging Face. If it doesn't know the answer, it will say so.")
33
+ return iface
34
+
35
+ iface = chat_interface()
36
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ transformers
3
+ gradio