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