rayyanphysicist commited on
Commit
29479ab
·
verified ·
1 Parent(s): 2ef50f5

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +113 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain.chat_models import ChatOpenAI
3
+ from langchain.llms import OpenAI
4
+ from langchain.prompts import PromptTemplate
5
+ from langchain.chains import LLMChain
6
+ from dotenv import load_dotenv
7
+ import os
8
+
9
+ load_dotenv()
10
+ KEY=os.getenv("OPENAI_API_KEY")
11
+ llm=ChatOpenAI(openai_api_key=KEY,model_name="gpt-3.5-turbo", temperature=0.3)
12
+
13
+ TEMPLATE="""
14
+ You 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
+
17
+ EXPLANATION_TEMPLATE="""
18
+ You are an expert programmer. Kindly explain this code: {text}
19
+ """
20
+
21
+ DEBUG_TEMPLATE="""
22
+ You are an expert coder. Kindly debug this code: {text}
23
+ """
24
+
25
+ def 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
+
40
+ def 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
+
53
+ def 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
67
+ st.title("Coding Assistant")
68
+
69
+ # User selects the operation
70
+ operation = st.selectbox("What do you want to do?", ["Code Generation", "Code Explanation", "Code Debugging"])
71
+
72
+ # User inputs
73
+ text = st.text_input("Enter the problem statement:")
74
+
75
+
76
+ if 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
+
104
+ elif 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)
109
+ elif 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)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ openai
2
+ langchain
3
+ streamlit
4
+ python-dotenv