Spaces:
Sleeping
Sleeping
rayyanphysicist
commited on
Commit
•
684eca4
1
Parent(s):
1aeb8f7
Upload 2 files
Browse files- app.py +92 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from langchain_mistralai import ChatMistralAI
|
4 |
+
|
5 |
+
def main():
|
6 |
+
st.title("Code Assistant with Mistral AI")
|
7 |
+
|
8 |
+
# Set API key
|
9 |
+
os.environ["MISTRAL_API_KEY"] = "4FRt1cK7cHt5MfFqTmiyxkQwOl9oN21F"
|
10 |
+
api_key = os.environ["MISTRAL_API_KEY"]
|
11 |
+
|
12 |
+
# Navigation
|
13 |
+
pages = ["Code Translation", "Code Generation", "Code Snippet Completion", "Code Documentation Generation"]
|
14 |
+
choice = st.sidebar.selectbox("Select Feature", pages)
|
15 |
+
|
16 |
+
if choice == "Code Translation":
|
17 |
+
code_translation(api_key)
|
18 |
+
elif choice == "Code Generation":
|
19 |
+
code_generation(api_key)
|
20 |
+
elif choice == "Code Snippet Completion":
|
21 |
+
code_snippet_completion(api_key)
|
22 |
+
elif choice == "Code Documentation Generation":
|
23 |
+
code_docs_generation(api_key)
|
24 |
+
|
25 |
+
def code_translation(api_key):
|
26 |
+
st.subheader("Code Translation")
|
27 |
+
source_code = st.text_area("Enter your source code")
|
28 |
+
languages = ["Python", "Java", "C++", "JavaScript", "Swift", "Ruby", "Kotlin"]
|
29 |
+
target_language = st.selectbox("Select target language", languages)
|
30 |
+
|
31 |
+
if st.button("Translate Code"):
|
32 |
+
llm = ChatMistralAI(model="codestral-latest", temperature=0, api_key=api_key)
|
33 |
+
prompt = f"Translate the following code to {target_language}:\n{source_code}"
|
34 |
+
response = llm.invoke([("user", prompt)])
|
35 |
+
content = response.content
|
36 |
+
translated_code = extract_code_block(content)
|
37 |
+
if translated_code:
|
38 |
+
st.code(translated_code)
|
39 |
+
else:
|
40 |
+
st.error("No translated code found in the response.")
|
41 |
+
|
42 |
+
def code_generation(api_key):
|
43 |
+
st.subheader("Code Generation")
|
44 |
+
code_description = st.text_area("Enter code description")
|
45 |
+
languages = ["Python", "Java", "C++", "JavaScript", "Swift", "Ruby", "Kotlin"]
|
46 |
+
target_language = st.selectbox("Select programming language", languages)
|
47 |
+
|
48 |
+
if st.button("Generate Code"):
|
49 |
+
llm = ChatMistralAI(model="codestral-latest", temperature=0, api_key=api_key)
|
50 |
+
prompt = f"{code_description} in {target_language}"
|
51 |
+
response = llm.invoke([("user", prompt)])
|
52 |
+
content = response.content
|
53 |
+
generated_code = extract_code_block(content)
|
54 |
+
if generated_code:
|
55 |
+
st.code(generated_code)
|
56 |
+
else:
|
57 |
+
st.error("No generated code found in the response.")
|
58 |
+
|
59 |
+
def code_snippet_completion(api_key):
|
60 |
+
st.subheader("Code Snippet Completion")
|
61 |
+
incomplete_code = st.text_area("Enter your incomplete code")
|
62 |
+
if st.button("Complete Code"):
|
63 |
+
llm = ChatMistralAI(model="codestral-latest", temperature=0, api_key=api_key)
|
64 |
+
prompt = f"Complete the following code snippet:\n{incomplete_code}"
|
65 |
+
response = llm.invoke([("user", prompt)])
|
66 |
+
content = response.content
|
67 |
+
completed_code = extract_code_block(content)
|
68 |
+
if completed_code:
|
69 |
+
st.code(completed_code)
|
70 |
+
else:
|
71 |
+
st.error("No completed code found in the response.")
|
72 |
+
|
73 |
+
def code_docs_generation(api_key):
|
74 |
+
st.subheader("Code Documentation Generation")
|
75 |
+
uploaded_file = st.file_uploader("Upload your code file", type=["py", "java", "cpp", "js", "swift", "rb", "kt"])
|
76 |
+
if uploaded_file is not None:
|
77 |
+
code = uploaded_file.read().decode("utf-8")
|
78 |
+
if st.button("Generate Documentation"):
|
79 |
+
llm = ChatMistralAI(model="codestral-latest", temperature=0, api_key=api_key)
|
80 |
+
prompt = f"Generate documentation for the following code:\n{code}"
|
81 |
+
response = llm.invoke([("user", prompt)])
|
82 |
+
content = response.content
|
83 |
+
st.markdown(content)
|
84 |
+
st.download_button("Download Documentation", content)
|
85 |
+
|
86 |
+
def extract_code_block(content):
|
87 |
+
import re
|
88 |
+
code_blocks = re.findall(r"```(.*?)```", content, re.DOTALL)
|
89 |
+
return code_blocks[0].strip() if code_blocks else None
|
90 |
+
|
91 |
+
if __name__ == "__main__":
|
92 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
langchain-mistralai
|
3 |
+
streamlit
|