rayyanphysicist/Coding_Assistant
0
1import streamlit as st
2from langchain.chat_models import ChatOpenAI
3from langchain.llms import OpenAI
4from langchain.prompts import PromptTemplate
5from langchain.chains import LLMChain
6from dotenv import load_dotenv
7import os
8
9load_dotenv()
10KEY=os.getenv("OPENAI_API_KEY")
11llm=ChatOpenAI(openai_api_key=KEY,model_name="gpt-3.5-turbo", temperature=0.3)
12
13TEMPLATE="""
14You are an expert in coding and solving problem. kindly help and solving problem. Iam learning how to code and want to see multiple approaches to solve problem. Please Code for {text} with both brute force and optimized approach, with clear time and space complexity of each, in {language}. Include explanation: {explain}
15"""
16
17EXPLANATION_TEMPLATE="""
18You are an expert programmer. Kindly explain this code: {text}
19"""
20
21DEBUG_TEMPLATE="""
22You are an expert coder. Kindly debug this code: {text}
23"""
24
25def generate_code(text, language, explain):
26 code_generation_prompt = PromptTemplate(
27 input_variables=["text", "language", "explain"],
28 template=TEMPLATE
29 )
30 code_chain = LLMChain(llm=llm, prompt=code_generation_prompt, output_key="code", verbose=True)
31 response = code_chain(
32 {
33 "text": text,
34 "language": language,
35 "explain": explain,
36 }
37 )
38 return response["code"]
39
40def generate_explanation(text):
41 explanation_prompt = PromptTemplate(
42 input_variables=["text"],
43 template=EXPLANATION_TEMPLATE
44 )
45 explanation_chain = LLMChain(llm=llm, prompt=explanation_prompt, output_key="explanation", verbose=True)
46 response = explanation_chain(
47 {
48 "text": text,
49 }
50 )
51 return response["explanation"]
52
53def debug_code(text):
54 debug_prompt = PromptTemplate(
55 input_variables=["text"],
56 template=DEBUG_TEMPLATE
57 )
58 debug_chain = LLMChain(llm=llm, prompt=debug_prompt, output_key="debug_info", verbose=True)
59 response = debug_chain(
60 {
61 "text": text,
62 }
63 )
64 return response["debug_info"]
65
66# Streamlit app
67st.title("Coding Assistant")
68
69# User selects the operation
70operation = st.selectbox("What do you want to do?", ["Code Generation", "Code Explanation", "Code Debugging"])
71
72# User inputs
73text = st.text_input("Enter the problem statement:")
74
75
76if operation == "Code Generation":
77 language = st.selectbox("Select the programming language:", ["C", "C++", "Java", "Python", "JavaScript", "Go"])
78 explain = st.checkbox("Include explanation")
79 if text:
80 code = generate_code(text, language, explain)
81
82 # Save history
83 if 'history' not in st.session_state:
84 st.session_state['history'] = []
85 st.session_state['history'].append(code)
86
87 # Display generated code
88 st.subheader("Generated Code:")
89 st.write(code, language=language)
90
91 # Add download button for the code
92 st.download_button(
93 label="Download Code",
94 data=code,
95 file_name='code.txt',
96 mime='text/plain',
97 )
98
99 # Display history
100 st.subheader("History:")
101 for i, item in enumerate(st.session_state['history']):
102 st.code(item, language=language)
103
104elif operation == "Code Explanation":
105 if text: # Check if the user has entered a problem statement
106 explanation = generate_explanation(text)
107 st.subheader("Generated Explanation:")
108 st.write(explanation)
109elif operation == "Code Debugging":
110 if text: # Check if the user has entered a problem statement
111 debug_info = debug_code(text)
112 st.subheader("Debug Information:")
113 st.write(debug_info)
114 