SHAMIL SHAHBAZ AWAN commited on
Commit
750cdab
Β·
verified Β·
1 Parent(s): 5d74ae9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -2,15 +2,15 @@ import streamlit as st
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  import torch
4
 
5
- # Load the fine-tuned MFTCoder model
6
  @st.cache_resource()
7
  def load_model():
8
- MODEL_NAME = "path-to-your-finetuned-model" # Replace with your MFTCoder fine-tuned model path
9
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
  model = AutoModelForCausalLM.from_pretrained(
11
- MODEL_NAME,
12
- torch_dtype=torch.float16, # Use float16 for performance optimization
13
- device_map="auto" # Automatically allocate to CPU/GPU
14
  )
15
  return pipeline("text-generation", model=model, tokenizer=tokenizer)
16
 
@@ -18,31 +18,31 @@ def load_model():
18
  code_generator = load_model()
19
 
20
  # Streamlit UI
21
- st.title("MFTCoder-powered Code Bot πŸš€")
22
- st.subheader("Generate high-quality code snippets with fine-tuned CodeLlama!")
23
 
24
  # User input
25
- prompt = st.text_area("Enter a code prompt to generate code:")
26
 
27
- # Generate code
28
  if st.button("Generate Code"):
29
  if prompt.strip():
30
  st.info("Generating code... Please wait ⏳")
31
  try:
32
- # Generate code using the fine-tuned MFTCoder model
33
  response = code_generator(
34
  prompt,
35
- max_length=256, # Adjust as needed
36
- temperature=0.3, # Lower temperature for accurate outputs
37
- num_return_sequences=1,
38
- do_sample=True
39
  )
40
  generated_code = response[0]['generated_text']
41
  # Display the code output
42
- st.code(generated_code, language="python") # Default to Python for generated output
43
  except Exception as e:
44
  st.error(f"Error: {str(e)}")
45
  else:
46
  st.warning("Please enter a prompt.")
47
 
48
- st.caption("Created by Shamil")
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  import torch
4
 
5
+ # Load the CodeLlama model and tokenizer
6
  @st.cache_resource()
7
  def load_model():
8
+ MODEL_NAME = "codellama/CodeLlama-7b-hf" # Model name
9
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
  model = AutoModelForCausalLM.from_pretrained(
11
+ MODEL_NAME,
12
+ torch_dtype=torch.float32, # Use float32 for CPU
13
+ device_map="cpu" # Map model to CPU
14
  )
15
  return pipeline("text-generation", model=model, tokenizer=tokenizer)
16
 
 
18
  code_generator = load_model()
19
 
20
  # Streamlit UI
21
+ st.title("CodeLlama-7B Code Bot πŸš€")
22
+ st.subheader("Generate code snippets using CodeLlama-7b-hf on CPU")
23
 
24
  # User input
25
+ prompt = st.text_area("Enter a coding prompt (e.g., 'Write a Python function to sort a list'): ")
26
 
27
+ # Generate Code
28
  if st.button("Generate Code"):
29
  if prompt.strip():
30
  st.info("Generating code... Please wait ⏳")
31
  try:
32
+ # Generate code using the CodeLlama model
33
  response = code_generator(
34
  prompt,
35
+ max_length=512, # Increase for longer code generation
36
+ temperature=0.2, # Lower temperature for more deterministic results
37
+ do_sample=True, # Enable sampling
38
+ num_return_sequences=1
39
  )
40
  generated_code = response[0]['generated_text']
41
  # Display the code output
42
+ st.code(generated_code, language="python") # Change language as needed
43
  except Exception as e:
44
  st.error(f"Error: {str(e)}")
45
  else:
46
  st.warning("Please enter a prompt.")
47
 
48
+ st.caption("Powered by CodeLlama-7B | Streamlit UI | CPU Optimized")