Rooobert commited on
Commit
59b677f
·
verified ·
1 Parent(s): dba9ef7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import google.generativeai as genai
3
+
4
+ # Configure Gemini API
5
+ genai.configure(api_key='AIzaSyCgfXYWyJjK8YrApUjO6433P3WAdpwQi0Y')
6
+ model = genai.GenerativeModel('gemini-pro')
7
+
8
+ def generate_response(user_message):
9
+ try:
10
+ # Use Gemini API to generate a response
11
+ response = model.generate_content(user_message)
12
+ # Check if the response has text
13
+ if response.text:
14
+ return response.text
15
+ else:
16
+ return "Error: The model generated an empty response."
17
+ except Exception as e:
18
+ return f"An error occurred: {str(e)}"
19
+
20
+ # Conversation history tracking
21
+ conversation_history = []
22
+
23
+ def chat_interface(user_message):
24
+ # Generate response
25
+ response = generate_response(user_message)
26
+
27
+ # Update conversation history
28
+ conversation_history.append((user_message, response))
29
+
30
+ # Prepare history for display
31
+ history_text = ""
32
+ for i, (question, answer) in enumerate(conversation_history):
33
+ history_text += f"Q{i+1}: {question}\n"
34
+ history_text += f"A{i+1}: {answer}\n\n"
35
+
36
+ return response, history_text
37
+
38
+ # Example questions
39
+ examples = [
40
+ "What is the capital of France?",
41
+ "Explain quantum computing in simple terms."
42
+ ]
43
+
44
+ # Create Gradio interface
45
+ with gr.Blocks() as demo:
46
+ gr.Markdown("# 🦷🦷 Gemini QA System 🦷🦷")
47
+ gr.Markdown("Ask a question and get an answer from Gemini AI.")
48
+
49
+ with gr.Row():
50
+ with gr.Column(scale=3):
51
+ input_text = gr.Textbox(label="Enter your question here...")
52
+ submit_btn = gr.Button("Submit")
53
+
54
+ with gr.Column(scale=1):
55
+ gr.Markdown("### Examples")
56
+ example_buttons = [gr.Button(ex) for ex in examples]
57
+
58
+ output_text = gr.Textbox(label="Answer")
59
+ history_text = gr.Textbox(label="Conversation History")
60
+
61
+ # Submit button logic
62
+ submit_btn.click(
63
+ fn=chat_interface,
64
+ inputs=input_text,
65
+ outputs=[output_text, history_text]
66
+ )
67
+
68
+ # Example button logic
69
+ for btn, ex in zip(example_buttons, examples):
70
+ btn.click(
71
+ fn=chat_interface,
72
+ inputs=gr.State(ex),
73
+ outputs=[output_text, history_text]
74
+ )
75
+
76
+ # Launch the demo
77
+ demo.launch()