thelip commited on
Commit
d91393a
·
verified ·
1 Parent(s): a58635e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -7
app.py CHANGED
@@ -1,12 +1,50 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!"
 
 
 
5
 
6
- demo = gr.Interface(lambda x: x, "image", "image")
 
 
 
7
 
8
- demo.launch(max_file_size="5mb")
9
- # or
10
- demo.launch(max_file_size=5 * gr.FileSize.MB)
 
 
 
 
 
 
 
 
 
11
 
12
- demo.launch(share=True) # Share your demo with just 1 extra parameter 🚀
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
5
+ # Load the LLaMA-2 model and tokenizer from Hugging Face
6
+ model_name = "meta-llama/Llama-2-7b-hf" # Change to the desired LLaMA model
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
9
+ model = model.to("cuda" if torch.cuda.is_available() else "cpu")
10
 
11
+ # Function to generate responses
12
+ def generate_response(user_input, chat_history):
13
+ # Add the user's input to the conversation history
14
+ chat_history.append({"role": "user", "content": user_input})
15
 
16
+ # Prepare input for the model
17
+ conversation = ""
18
+ for turn in chat_history:
19
+ conversation += f"{turn['role']}: {turn['content']}\n"
20
+ inputs = tokenizer(conversation, return_tensors="pt").to(model.device)
21
+
22
+ # Generate model response
23
+ outputs = model.generate(inputs.input_ids, max_length=500, do_sample=True, temperature=0.7)
24
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
25
+
26
+ # Add the model's response to the chat history
27
+ chat_history.append({"role": "assistant", "content": response})
28
 
29
+ # Only return the model's response for display
30
+ return response, chat_history
31
+
32
+ # Initialize the chat history
33
+ chat_history = []
34
+
35
+ # Define Gradio interface
36
+ with gr.Blocks() as chat_interface:
37
+ gr.Markdown("## LLaMA-2 Chatbot")
38
+ chat_input = gr.Textbox(label="Your Message")
39
+ chat_output = gr.Chatbot()
40
+
41
+ # Update chat on button click
42
+ def handle_input(user_input):
43
+ response, chat_history = generate_response(user_input, chat_history)
44
+ chat_output.update(chat_history)
45
+ return "", chat_history # Clear input box and update chat history
46
+
47
+ chat_input.submit(handle_input, inputs=chat_input, outputs=[chat_input, chat_output])
48
+
49
+ # Launch Gradio app
50
+ chat_interface.launch()