ayush5710 commited on
Commit
f73ac0d
Β·
1 Parent(s): 78f97d6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+ import os
4
+ import requests
5
+ from bardapi import Bard
6
+
7
+ # Load the GOOGLE_LANGUAGES_TO_CODES dictionary from lang.json
8
+ with open("lang.json", "r") as file:
9
+ GOOGLE_LANGUAGES_TO_CODES = json.load(file)
10
+
11
+ with st.sidebar:
12
+ # Add a selector in the sidebar using the dictionary's keys
13
+ selected_language_name = st.sidebar.selectbox("Select Language", list(GOOGLE_LANGUAGES_TO_CODES.keys()))
14
+ code_interpreter = st.sidebar.toggle("Code Interpreter", value=True)
15
+ system_prompt = st.sidebar.text_input("System prompt for code interpreter", value = "Rule 1: If a user provides a code explain it line by line")
16
+ useSystemPrompt = st.sidebar.toggle("Use System prompt", value=True)
17
+ exportToReplIt = st.sidebar.toggle("Export to repl.it", value=False)
18
+ showImages = st.sidebar.toggle("Show images", value=True)
19
+
20
+ # Retrieve the corresponding language code from the dictionary
21
+ selected_language_code = GOOGLE_LANGUAGES_TO_CODES[selected_language_name]
22
+
23
+ # Initialize Bard with the selected language code
24
+ bard = Bard(token=os.getenv("_BARD_API_KEY"), language=selected_language_code)
25
+
26
+ TITLE = "Ayush Dhawan's Codebot"
27
+ DESCRIPTION = """
28
+ Welcome to my coding chatbot! paste any code you want an explanation for!
29
+ """
30
+
31
+
32
+ # Streamlit UI
33
+ st.title(TITLE)
34
+ st.write(DESCRIPTION)
35
+
36
+ # Prediction function
37
+ def predict(message):
38
+ with st.status("Requesting πŸ¦”..."):
39
+ st.write("Requesting API...")
40
+ response = bard.get_answer(message if not (code_interpreter and useSystemPrompt) else message + " . "+system_prompt)
41
+ st.write("Done...")
42
+
43
+ st.write("Checking images...")
44
+ if 'images' in response.keys() and showImages:
45
+ for i in response['images']:
46
+ st.image(i)
47
+
48
+ return response
49
+
50
+ # Display chat messages from history on app rerun
51
+ if "messages" not in st.session_state:
52
+ st.session_state.messages = []
53
+
54
+ for message in st.session_state.messages:
55
+ with st.chat_message(message["role"], avatar=("πŸ§‘β€πŸ’»" if message["role"] == 'human' else 'πŸ¦”')):
56
+ st.markdown(message["content"])
57
+
58
+ # React to user input
59
+ if prompt := st.chat_input("Ask Palm 2 anything..."):
60
+ st.chat_message("human", avatar="πŸ§‘β€πŸ’»").markdown(prompt)
61
+ st.session_state.messages.append({"role": "human", "content": prompt})
62
+
63
+ response = predict(prompt)
64
+ with st.chat_message("assistant", avatar='πŸ¦”'):
65
+ st.markdown(response['content'])
66
+
67
+ if response['code']:
68
+ if exportToReplIt:
69
+ with st.status("Exporting replit..."):
70
+ fale = False
71
+ try:
72
+ url = bard.export_replit(code=response['code'],program_lang=response['program_lang'])['url']
73
+ except error:
74
+ fale=True
75
+ st.write('ERROR')
76
+ if not fale:
77
+ st.title('Export to repl.it')
78
+ st.markdown(f'[link]({url})')
79
+ if code_interpreter:
80
+ try:
81
+ exec(response['code'])
82
+ except Exception as e:
83
+ st.write(f"ERROR {e}...")
84
+
85
+ st.session_state.messages.append({"role": "assistant", "content": response['content']})