shljessie commited on
Commit
3c3897d
·
1 Parent(s): f562f78

convert to original

Browse files
Files changed (1) hide show
  1. app.py +4 -56
app.py CHANGED
@@ -1,59 +1,7 @@
1
- import os
2
- import threading
3
  import gradio as gr
4
- import torch
5
- from transformers import AutoModelForCausalLM, AutoTokenizer
6
- import subprocess
7
- import sys
8
 
9
- def install_requirements():
10
- print('Installing Requirements')
11
- subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt'])
12
 
13
- # Check if CUDA is available
14
- if not torch.cuda.is_available():
15
- raise EnvironmentError("CUDA is not available. This script requires a GPU.")
16
-
17
- # Model Configuration
18
- MODEL_ID = "meta-llama/Llama-2-7b-chat"
19
- MAX_INPUT_TOKEN_LENGTH = 4096
20
- MAX_NEW_TOKENS = 1024
21
- TEMPERATURE = 0.6
22
- TOP_P = 0.9
23
- TOP_K = 50
24
- REPETITION_PENALTY = 1.2
25
-
26
- # Load the model and tokenizer
27
- model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float16, device_map="auto")
28
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
29
-
30
- def generate_response(user_input):
31
- """
32
- Generate a response to the user input using the Llama-2 7B model.
33
- """
34
- input_ids = tokenizer.encode(user_input, return_tensors="pt")
35
- input_ids = input_ids.to(model.device)
36
-
37
- # Generate a response
38
- output = model.generate(input_ids, max_length=MAX_INPUT_TOKEN_LENGTH + len(input_ids[0]),
39
- max_new_tokens=MAX_NEW_TOKENS, temperature=TEMPERATURE,
40
- top_k=TOP_K, top_p=TOP_P, repetition_penalty=REPETITION_PENALTY)
41
-
42
- response = tokenizer.decode(output[0], skip_special_tokens=True)
43
- return response
44
-
45
- def chatbot_interface(user_input):
46
- return generate_response(user_input)
47
-
48
- # Create the Gradio interface
49
- iface = gr.Interface(
50
- fn=chatbot_interface,
51
- inputs=gr.inputs.Textbox(lines=2, placeholder="Type your message here..."),
52
- outputs="text",
53
- title="Llama-2 7B Chatbot",
54
- description="This is a chatbot powered by the Llama-2 7B model. Try asking it something!",
55
- )
56
-
57
- if __name__ == "__main__":
58
- install_requirements()
59
- iface.launch()
 
 
 
1
  import gradio as gr
 
 
 
 
2
 
3
+ def greet(name):
4
+ return "Hello " + name + "!!"
 
5
 
6
+ iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
+ iface.launch()