Canstralian commited on
Commit
51f615c
·
verified ·
1 Parent(s): 891214c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -43
app.py CHANGED
@@ -1,44 +1,56 @@
1
- # Initialize Replit LLM Pipeline
2
- try:
3
- nlp_pipeline = pipeline("text2text-generation", model=model_name)
4
- st.success("Model loaded successfully!")
5
- except Exception as e:
6
- st.error(f"Error loading model: {e}. Please verify the model name or your internet connection.")
7
- st.stop()
8
-
9
- # Input Section
10
- st.subheader("Input Your Query")
11
- user_input = st.text_area("Enter your query or task description", height=150)
12
-
13
- # Submit Button
14
- if st.button("Generate Output"):
15
- if user_input.strip() == "":
16
- st.warning("Please enter a valid input.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  else:
18
- with st.spinner("Processing..."):
19
- try:
20
- # Generate response using Replit LLM
21
- output = nlp_pipeline(user_input)
22
- response = output[0]["generated_text"]
23
- st.subheader("AI Response")
24
- st.write(response)
25
- except Exception as e:
26
- st.error(f"An error occurred: {e}")
27
-
28
- # Handling Code Optimization:
29
- if task_type == "Code Optimization":
30
- st.info("Paste your Python code for optimization recommendations.")
31
- user_code = st.text_area("Paste your Python code", height=200)
32
- if st.button("Optimize Code"):
33
- if user_code.strip() == "":
34
- st.warning("Please paste valid Python code to optimize.")
35
- else:
36
- with st.spinner("Analyzing and optimizing..."):
37
- try:
38
- optimization_prompt = f"Optimize the following Python code:\n\n{user_code}"
39
- output = nlp_pipeline(optimization_prompt)
40
- optimized_code = output[0]["generated_text"]
41
- st.subheader("Optimized Code")
42
- st.code(optimized_code)
43
- except Exception as e:
44
- st.error(f"An error occurred while optimizing code: {e}")
 
1
+ import streamlit as st
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+
4
+ # Load the model and tokenizer
5
+ @st.cache_resource
6
+ def load_model():
7
+ model_name = "replit/replit-code-v1-3b" # Replace with your fine-tuned model if applicable
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
10
+ return model, tokenizer
11
+
12
+ model, tokenizer = load_model()
13
+
14
+ # App title and description
15
+ st.title("Replit-code-v1-3b Code Assistant 📊")
16
+ st.markdown("""
17
+ This application allows you to interact with the **Replit-code-v1-3b** large language model.
18
+ You can use it to generate, debug, or optimize code snippets.
19
+ Simply provide a prompt, and the model will respond with suggestions!
20
+ """)
21
+
22
+ # User input
23
+ st.header("Enter Your Prompt")
24
+ prompt = st.text_area("Describe your coding task or paste your code for debugging:")
25
+
26
+ # Temperature and max length controls
27
+ st.sidebar.header("Model Settings")
28
+ temperature = st.sidebar.slider("Temperature (Creativity)", 0.0, 1.0, 0.7)
29
+ max_length = st.sidebar.slider("Max Response Length", 50, 500, 200)
30
+
31
+ # Submit button
32
+ if st.button("Generate Response"):
33
+ if prompt.strip():
34
+ with st.spinner("Generating response..."):
35
+ # Tokenize and generate response
36
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
37
+ outputs = model.generate(
38
+ inputs.input_ids,
39
+ max_length=max_length,
40
+ temperature=temperature,
41
+ pad_token_id=tokenizer.eos_token_id
42
+ )
43
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
44
+
45
+ # Display response
46
+ st.subheader("Generated Code/Response")
47
+ st.code(response, language="python")
48
  else:
49
+ st.warning("Please enter a prompt to generate a response.")
50
+
51
+ # Footer
52
+ st.markdown("---")
53
+ st.markdown("""
54
+ **Replit-code-v1-3b Code Assistant**
55
+ Built with [Streamlit](https://streamlit.io/) and the [Hugging Face Transformers Library](https://huggingface.co/docs/transformers).
56
+ """)