suriya7 commited on
Commit
cbcf653
1 Parent(s): abcff02

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from streamlit_chat import message
4
+ import google.generativeai as genai
5
+ from langchain.prompts import PromptTemplate
6
+ from langchain import LLMChain
7
+ from langchain_google_genai import ChatGoogleGenerativeAI
8
+
9
+ genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
10
+
11
+ llm = ChatGoogleGenerativeAI(model="gemini-pro",
12
+ temperature=0.7)
13
+
14
+
15
+ template = """You are a chatbot called "CRETA" having a conversation with a human and you are created by suriya an AI Enthusiastic.
16
+ previous_chat:
17
+ {chat_history}
18
+ Human: {human_input}
19
+ Chatbot:"""
20
+
21
+ prompt = PromptTemplate(
22
+ input_variables=["chat_history", "human_input"], template=template
23
+ )
24
+
25
+ llm_chain = LLMChain(
26
+ llm=llm,
27
+ prompt=prompt,
28
+ verbose=True,
29
+ )
30
+
31
+
32
+ previous_response = ""
33
+ def conversational_chat(query):
34
+ global previous_response
35
+ for i in st.session_state['history']:
36
+ if i is not None:
37
+ previous_response += f"Human: {i[0]}\n Chatbot: {i[1]}"
38
+ print(previous_response)
39
+ result = llm_chain.predict(chat_history=previous_response, human_input=query)
40
+ st.session_state['history'].append((query, result))
41
+ return result
42
+
43
+ st.title("ASSISTANT BOT:")
44
+
45
+ if 'history' not in st.session_state:
46
+ st.session_state['history'] = []
47
+
48
+ # Initialize messages
49
+ if 'generated' not in st.session_state:
50
+ st.session_state['generated'] = ["Hello ! Ask me anything"]
51
+
52
+ if 'past' not in st.session_state:
53
+ st.session_state['past'] = [" "]
54
+
55
+ # Create containers for chat history and user input
56
+ response_container = st.container()
57
+ container = st.container()
58
+
59
+ # User input form
60
+ user_input = st.chat_input("Ask Your Questions 👉..")
61
+ with container:
62
+ if user_input:
63
+ output = conversational_chat(user_input)
64
+ # answer = response_generator(output)
65
+ st.session_state['past'].append(user_input)
66
+ st.session_state['generated'].append(output)
67
+
68
+ # Display chat history
69
+ if st.session_state['generated']:
70
+ with response_container:
71
+ for i in range(len(st.session_state['generated'])):
72
+ if i != 0:
73
+ message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="adventurer")
74
+ message(st.session_state["generated"][i], key=str(i), avatar_style="bottts")