Alpersx commited on
Commit
0028558
·
verified ·
1 Parent(s): 9453ce9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import google.generativeai as genai # Import the Google Generative AI model
2
+ import gradio as gr # Import Gradio to create a web-based user interface (UI)
3
+ from api_read import GEMINI_API_KEY # Import the API key for Gemini API from an external file
4
+
5
+ # Configure Google Generative AI with the API key
6
+ genai.configure(api_key=GEMINI_API_KEY) # Set the API key for authentication
7
+
8
+ # Initialize the generative model
9
+ model = genai.GenerativeModel("gemini-1.5-pro") # Create a model instance using Gemini's specific version
10
+
11
+ def chat_interface(user_input, image_input, chat_state):
12
+ # Check if the chat_state is None (first time interaction)
13
+ if chat_state is None:
14
+ chat_state = {"history": []} # Initialize an empty chat history
15
+
16
+ # If user input is empty, return an empty response and the chat history
17
+ if not user_input.strip():
18
+ # Curate history from the existing conversation and return
19
+ messages = [{"role": "user", "content": u} for u, a in chat_state["history"]]
20
+ messages += [{"role": "assistant", content: a} for u, a in chat_state["history"]]
21
+ curated_history = "\n\n".join([f"*user*: {u}\n\n*assistant*: {a}" for u, a in chat_state["history"]])
22
+ return "", messages, curated_history, chat_state
23
+
24
+ # Create a prompt for the model by combining chat history and user input
25
+ prompt = "\n".join([f"User: {u}\nAssistant: {a}" for u, a in chat_state["history"]])
26
+ prompt += f"\nUser: {user_input}\nAssistant: "
27
+
28
+ # If an image is provided, include it in the prompt for the model to process
29
+ if image_input:
30
+ response = model.generate_content([image_input, prompt])
31
+ else:
32
+ # If no image is provided, just send the prompt text to the model
33
+ response = model.generate_content(prompt)
34
+
35
+ # Get the model's response (assistant's reply)
36
+ reply = response.text
37
+
38
+ # Update the chat history with the new user input and assistant's reply
39
+ chat_state["history"].append((user_input, reply))
40
+
41
+ # Prepare messages for the chatbot UI
42
+ messages = [{"role": "user", "content": u} for u, a in chat_state["history"]]
43
+ messages +=[{"role": "assistant", "content": a} for u, a in chat_state["history"]]
44
+
45
+ # Curate and format the chat history
46
+ curated_history = "\n\n".join(f"*user*: {u}\n*assistant*: {a}" for u, a in chat_state["history"])
47
+
48
+ # Return updated UI elements : empty input box, chatbot messages, chat history, and updated chat state
49
+ return "", messages, curated_history, chat_state
50
+
51
+ # Create Gradio UI for the chat_interface
52
+ with gr.Blocks() as demo:
53
+ gr.Markdown("##Gemini 2.0 Flash Chat") # Add title to the UI
54
+
55
+ chat_state = gr.State() # Store the current state of the chat, including history
56
+
57
+ with gr.Row(): # Organize elements in a row (side by side)
58
+ with gr.Column(): # First column for the chat area
59
+ chatbot = gr.Chatbot(label="Conversation", type="messages") # Display chat messages
60
+ user_input = gr.Textbox(label="Your Message") # Input box for user messages
61
+ submit_btn = gr.Button("Gönder") # Submit button for sending messages
62
+ attach_button = gr.Button("Resim Ekle", scale=1) # Button to attach images
63
+ image_input = gr.Image(type="pil", label="Resim Yükle", visible=False, scale=1) # Hidden image upload area
64
+
65
+ with gr.Column(): # Second column for displaying the raw chat history
66
+ raw_history = gr.Markdown("*Conversation History") # Display formatted chat history
67
+
68
+ # Define the function to toggle the visibility of the image upload area
69
+ def toggle_image_upload():
70
+ return gr.update(visible=True) # Make the image upload area visible
71
+
72
+ # Link the attach button to the toggle_image_upload function
73
+ attach_button.click(toggle_image_upload, [], [image_input])
74
+
75
+ # Set the submit button to trigger the chat_interface function with inputs
76
+ submit_btn.click(chat_interface, [user_input, image_input, chat_state], [user_input, chatbot, raw_history, chat_state])
77
+
78
+ # Set the textbox to trigger the chat_interface function when Enter is pressed
79
+ user_input.submit(chat_interface, [user_input, image_input, chat_state], [user_input, chatbot, raw_history, chat_state])
80
+
81
+ # Launch the Gradio interface (web app)
82
+ demo.launch()
83
+
84
+
85
+
86
+
87
+