techguy1423 commited on
Commit
9d955c2
·
1 Parent(s): c956efe

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. test4.py +38 -0
test4.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # Load the pre-trained Llama model and tokenizer
6
+ model_name = "meta-llama/Llama-2-13b-chat-hf"
7
+ tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-13b-chat-hf")
8
+ model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-13b-chat-hf")
9
+
10
+ # Define a system prompt to set the context and behavior
11
+ system_prompt = "You are chatting with a friendly AI. Ask me anything!"
12
+
13
+ # Function to generate a response
14
+ def chat(input_text):
15
+ # Combine the system prompt and user input
16
+ full_prompt = f"{system_prompt}\n\n{input_text}"
17
+
18
+ # Encode the combined prompt and generate a response
19
+ input_ids = tokenizer.encode(full_prompt, return_tensors="pt")
20
+ with torch.no_grad():
21
+ output = model.generate(input_ids, max_length=50, num_return_sequences=1)
22
+
23
+ # Decode and return the AI's response
24
+ ai_response = tokenizer.decode(output[0], skip_special_tokens=True)
25
+ return ai_response
26
+
27
+ # Create a Gradio interface
28
+ iface = gr.Interface(
29
+ fn=chat,
30
+ inputs="text",
31
+ outputs="text",
32
+ title="Llama Chatbot",
33
+ description="Chat with a friendly AI chatbot powered by the Llama model.",
34
+ live=True
35
+ )
36
+
37
+ # Launch the Gradio interface
38
+ iface.launch()