Skip to content

Instantly share code, notes, and snippets.

@rohitrajiit
Created December 15, 2023 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohitrajiit/2fe090457750cbc026e46e8c60d28d6e to your computer and use it in GitHub Desktop.
Save rohitrajiit/2fe090457750cbc026e46e8c60d28d6e to your computer and use it in GitHub Desktop.
import gradio as gr
import google.generativeai as genai
apikey = 'xxx' # Replace with your key
genai.configure(api_key=apikey)
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.ClearButton([msg, chatbot])
model = genai.GenerativeModel('gemini-pro')
def respond(message, chat_history):
history_gemini_format = []
for user, assistant in chat_history:
history_gemini_format.append({'role':'user',
'parts':[user]})
history_gemini_format.append({'role':'model',
'parts':[assistant]})
history_gemini_format.append({'role':'user',
'parts':[message]})
print(history_gemini_format)
response = model.generate_content(history_gemini_format)
responsetext = response.text
chat_history.append((message, responsetext))
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
demo.launch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment