Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,30 @@
|
|
1 |
-
from transformers import
|
2 |
-
import transformers
|
3 |
-
import torch
|
4 |
|
5 |
-
model
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
"text-generation",
|
10 |
-
model=model,
|
11 |
-
torch_dtype=torch.float16,
|
12 |
-
device_map="auto",
|
13 |
-
)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
)
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
|
|
|
|
2 |
|
3 |
+
# Load pre-trained model and tokenizer
|
4 |
+
model_name = "codellama/CodeLlama-7b-hf"
|
5 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
6 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
7 |
|
8 |
+
# System message
|
9 |
+
system_message = "You are a code teaching assistant named OmniCode created by Anusha K. Answer all the code related questions being asked."
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
def generate_response(prompt, max_length=150, temperature=1.0):
|
12 |
+
input_text = system_message + "\n" + prompt
|
13 |
+
input_ids = tokenizer.encode(input_text, return_tensors='pt')
|
14 |
+
|
15 |
+
# Generate response
|
16 |
+
output = model.generate(input_ids,
|
17 |
+
max_length=max_length,
|
18 |
+
temperature=temperature,
|
19 |
+
pad_token_id=tokenizer.eos_token_id,
|
20 |
+
num_return_sequences=1)
|
21 |
+
|
22 |
+
# Decode and return the response
|
23 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
24 |
+
return response
|
25 |
+
|
26 |
+
if __name__ == "__main__":
|
27 |
+
while True:
|
28 |
+
user_input = input("You: ")
|
29 |
+
response = generate_response(user_input)
|
30 |
+
print("OmniCode:", response)
|