acecalisto3 commited on
Commit
2cdc6c2
·
verified ·
1 Parent(s): 423578e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -41
app.py CHANGED
@@ -1,56 +1,37 @@
1
  import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import logging
4
  import torch
5
- from functools import lru_cache
6
 
7
  # Logging Setup
8
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
9
 
10
- # Cache the model and tokenizer loading
11
- @lru_cache(maxsize=None)
12
- def load_model_and_tokenizer(model_name):
13
- try:
14
- tokenizer = AutoTokenizer.from_pretrained(model_name)
15
- model = AutoModelForCausalLM.from_pretrained(model_name)
16
- tokenizer.pad_token = tokenizer.eos_token
17
- return model, tokenizer
18
- except Exception as e:
19
- logging.error(f"Error loading model and tokenizer: {e}")
20
- return None, None
21
-
22
  # Initialize session state
23
  if 'generated_codes' not in st.session_state:
24
  st.session_state.generated_codes = []
25
 
 
 
 
 
 
 
 
 
 
26
  @st.cache_data
27
  def generate_code(task_description, max_length, temperature, num_return_sequences, model_name):
28
- model, tokenizer = load_model_and_tokenizer(model_name)
29
- if model is None or tokenizer is None:
30
- return ["Error: Failed to load model and tokenizer."]
31
-
32
  try:
33
  logging.info(f"Generating code with input: {task_description}")
34
  prompt = f"Develop code for the following task: {task_description}"
35
 
36
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
37
- max_new_tokens = max(max_length - inputs.input_ids.shape[1], 1) # Ensure max_new_tokens is at least 1
38
-
39
- with torch.no_grad():
40
- output = model.generate(
41
- inputs.input_ids,
42
- max_new_tokens=max_new_tokens,
43
- num_return_sequences=num_return_sequences,
44
- temperature=temperature,
45
- do_sample=True,
46
- no_repeat_ngram_size=2,
47
- top_k=50,
48
- top_p=0.95,
49
- pad_token_id=tokenizer.eos_token_id,
50
- attention_mask=inputs.attention_mask,
51
- )
52
 
53
- codes = [tokenizer.decode(seq, skip_special_tokens=True) for seq in output]
54
  logging.info("Code generation completed successfully.")
55
  return codes
56
  except Exception as e:
@@ -73,11 +54,13 @@ def main():
73
  st.markdown("This application generates code based on the given task description using a text-generation model.")
74
 
75
  # Model Selection
76
- model_name = st.selectbox(
77
- "Select Model",
78
- ["EleutherAI/gpt-neo-2.7B", "EleutherAI/gpt-j-6B"],
79
- help="Choose the model for code generation."
80
- )
 
 
81
 
82
  # Input Section
83
  st.header("Task Description")
@@ -87,7 +70,7 @@ def main():
87
  st.header("Options")
88
  col1, col2, col3 = st.columns(3)
89
  with col1:
90
- max_length = st.slider("Max Length", min_value=50, max_value=2048, value=2000, step=25, help="Maximum length of the generated code.")
91
  with col2:
92
  temperature = st.slider("Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.1, help="Controls the creativity of the generated code.")
93
  with col3:
 
1
  import streamlit as st
2
+ from transformers import pipeline
3
  import logging
4
  import torch
 
5
 
6
  # Logging Setup
7
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
8
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  # Initialize session state
10
  if 'generated_codes' not in st.session_state:
11
  st.session_state.generated_codes = []
12
 
13
+ @st.cache_resource
14
+ def get_model_pipeline(model_name):
15
+ try:
16
+ code_pipeline = pipeline("text-generation", model=model_name, device=0 if torch.cuda.is_available() else -1)
17
+ return code_pipeline
18
+ except Exception as e:
19
+ logging.error(f"Error loading model pipeline: {e}")
20
+ return None
21
+
22
  @st.cache_data
23
  def generate_code(task_description, max_length, temperature, num_return_sequences, model_name):
24
+ code_pipeline = get_model_pipeline(model_name)
25
+ if code_pipeline is None:
26
+ return ["Error: Failed to load model pipeline."]
27
+
28
  try:
29
  logging.info(f"Generating code with input: {task_description}")
30
  prompt = f"Develop code for the following task: {task_description}"
31
 
32
+ outputs = code_pipeline(prompt, max_length=max_length, num_return_sequences=num_return_sequences, temperature=temperature)
33
+ codes = [output['generated_text'] for output in outputs]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
 
35
  logging.info("Code generation completed successfully.")
36
  return codes
37
  except Exception as e:
 
54
  st.markdown("This application generates code based on the given task description using a text-generation model.")
55
 
56
  # Model Selection
57
+ model_name = st.selectbox("Select Model", [
58
+ "EleutherAI/gpt-neo-2.7B",
59
+ "EleutherAI/gpt-j-6B",
60
+ "bigscience/bloom-1b7",
61
+ "openai-gpt",
62
+ "gpt2-xl"
63
+ ], help="Choose the model for code generation.")
64
 
65
  # Input Section
66
  st.header("Task Description")
 
70
  st.header("Options")
71
  col1, col2, col3 = st.columns(3)
72
  with col1:
73
+ max_length = st.slider("Max Length", min_value=50, max_value=1000, value=250, step=50, help="Maximum length of the generated code.")
74
  with col2:
75
  temperature = st.slider("Temperature", min_value=0.1, max_value=1.0, value=0.7, step=0.1, help="Controls the creativity of the generated code.")
76
  with col3: