Download app.py from Ankit93/Chat: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/spaces/Ankit93/Chat/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ankit93/Chat/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ankit93/Chat/resolve/main/app.py
1.65 kB
| import pathlib | |
| import textwrap | |
| import streamlit as st | |
| import google.generativeai as genai | |
| from dotenv import load_dotenv | |
| from IPython.display import Markdown | |
| from IPython.display import display | |
| import os | |
| load_dotenv() | |
| genai.configure(api_key="AIzaSyA_XykKxC4aSi0af9VH5uP2eQlp9Nh25Ds") | |
| #model = genai.GenerativeModel(model_name="models/gemini-pro") | |
| model = genai.GenerativeModel("gemini-pro") | |
| chat = model.start_chat(history=[]) | |
| def to_markdown(text): | |
| text = text.replace('•', ' *') | |
| return Markdown(textwrap.indent(text,'> ',predicate=lambda _:True)) | |
| def get_gemini_response(question): | |
| #response = model.generate_content(question) | |
| question = question +str(' Answer shortly') | |
| response = chat.send_message(question,stream=True) | |
| #to_markdown(response.text) | |
| #print('Response:',response.text) | |
| #response = to_markdown(response.text) | |
| return response | |
| st.title("Google Powered Chatbot") | |
| if 'chat_history' not in st.session_state: | |
| st.session_state['chat_history'] = [] | |
| user_input = st.text_input("Input:",key='input') | |
| submit_button = st.button("Ask Question") | |
| if submit_button and user_input: | |
| if user_input: | |
| bot_response = get_gemini_response(user_input) | |
| st.session_state['chat_history'].append(("You",user_input)) | |
| for chunk in bot_response: | |
| st.write(chunk.text) | |
| st.session_state['chat_history'].append(("Bot",chunk.text)) | |
| #st.write("Bot:", bot_response) | |
| else: | |
| st.warning("Please enter something.") | |
| # Function to interact with OpenAI API and get response | |
| for role,text in st.session_state['chat_history']: | |
| st.write(f"{role}:{text}") | |