Spaces:
Sleeping
Sleeping
Create chat.py
Browse files
chat.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Q&A Chatbot
|
2 |
+
#from langchain.llms import OpenAI
|
3 |
+
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
load_dotenv() # take environment variables from .env.
|
7 |
+
|
8 |
+
import streamlit as st
|
9 |
+
import os
|
10 |
+
import pathlib
|
11 |
+
import textwrap
|
12 |
+
|
13 |
+
import google.generativeai as genai
|
14 |
+
|
15 |
+
from IPython.display import display
|
16 |
+
from IPython.display import Markdown
|
17 |
+
|
18 |
+
|
19 |
+
os.getenv("GOOGLE_API_KEY")
|
20 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
21 |
+
|
22 |
+
## Function to load OpenAI model and get respones
|
23 |
+
model = genai.GenerativeModel('gemini-pro')
|
24 |
+
chat = model.start_chat(history=[])
|
25 |
+
def get_gemini_response(question):
|
26 |
+
|
27 |
+
response =chat.send_message(question,stream=True)
|
28 |
+
return response
|
29 |
+
|
30 |
+
##initialize our streamlit app
|
31 |
+
|
32 |
+
st.set_page_config(page_title="Q&A Demo")
|
33 |
+
|
34 |
+
st.header("Gemini Application")
|
35 |
+
|
36 |
+
input=st.text_input("Input: ",key="input")
|
37 |
+
|
38 |
+
|
39 |
+
submit=st.button("Ask the question")
|
40 |
+
|
41 |
+
## If ask button is clicked
|
42 |
+
|
43 |
+
if submit:
|
44 |
+
|
45 |
+
response=get_gemini_response(input)
|
46 |
+
st.subheader("The Response is")
|
47 |
+
for chunk in response:
|
48 |
+
print(st.write(chunk.text))
|
49 |
+
print("_"*80)
|
50 |
+
|
51 |
+
st.write(chat.history)
|