johnpaulbin commited on
Commit
200f09a
1 Parent(s): 3f5c25d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import json
3
  import os
 
4
 
5
  # Function to get available JSON files in the working directory
6
  def get_available_weeks():
@@ -40,6 +41,45 @@ def change_question(week_file, index, direction):
40
  index = (index + direction) % total
41
  return index, *flashcard_ui(week_file, index)
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  # Gradio interface
44
  with gr.Blocks() as demo:
45
  available_weeks = get_available_weeks()
@@ -59,6 +99,12 @@ with gr.Blocks() as demo:
59
  prev_btn = gr.Button("Previous")
60
  reveal_btn = gr.Button("Reveal Answer")
61
  next_btn = gr.Button("Next")
 
 
 
 
 
 
62
 
63
  # Start button action to reveal the flashcard section
64
  start_button.click(lambda: gr.update(visible=True), inputs=[], outputs=flashcard_section)
@@ -69,5 +115,8 @@ with gr.Blocks() as demo:
69
  prev_btn.click(change_question, inputs=[week_file, index, gr.Number(-1, visible=False)], outputs=[index, question_output, answer_output])
70
  next_btn.click(change_question, inputs=[week_file, index, gr.Number(1, visible=False)], outputs=[index, question_output, answer_output])
71
 
 
 
 
72
  # Launch the app
73
  demo.launch()
 
1
  import gradio as gr
2
  import json
3
  import os
4
+ import google.generativeai as genai
5
 
6
  # Function to get available JSON files in the working directory
7
  def get_available_weeks():
 
41
  index = (index + direction) % total
42
  return index, *flashcard_ui(week_file, index)
43
 
44
+ with open("aa2.txt", "r") as f:
45
+ prompt = f.read()
46
+
47
+ # AI Chatbot function
48
+ def chat_with_ai(user_input):
49
+ genai.configure(api_key=os.environ["GEMINI_API_KEY"])
50
+
51
+ # Create the model configuration
52
+ generation_config = {
53
+ "temperature": 0.5,
54
+ "top_p": 0.98,
55
+ "top_k": 64,
56
+ "max_output_tokens": 8192,
57
+ "response_mime_type": "text/plain",
58
+ }
59
+
60
+ model = genai.GenerativeModel(
61
+ model_name="gemini-1.5-flash",
62
+ generation_config=generation_config,
63
+ )
64
+
65
+ chat_session = model.start_chat(
66
+ history=[
67
+ {
68
+ "role": "user",
69
+ "parts": [ prompt ],
70
+ },
71
+ {
72
+ "role": "model",
73
+ "parts": [
74
+ "Sure, I can answer your question. \n",
75
+ ],
76
+ },
77
+ ]
78
+ )
79
+
80
+ response = chat_session.send_message(user_input)
81
+ return response.text
82
+
83
  # Gradio interface
84
  with gr.Blocks() as demo:
85
  available_weeks = get_available_weeks()
 
99
  prev_btn = gr.Button("Previous")
100
  reveal_btn = gr.Button("Reveal Answer")
101
  next_btn = gr.Button("Next")
102
+
103
+ # AI Chatbot UI
104
+ with gr.Column(visible=True):
105
+ chat_input = gr.Textbox(label="Ask a question to the AI")
106
+ chat_output = gr.Textbox(label="AI Response", interactive=False)
107
+ chat_button = gr.Button("Ask AI")
108
 
109
  # Start button action to reveal the flashcard section
110
  start_button.click(lambda: gr.update(visible=True), inputs=[], outputs=flashcard_section)
 
115
  prev_btn.click(change_question, inputs=[week_file, index, gr.Number(-1, visible=False)], outputs=[index, question_output, answer_output])
116
  next_btn.click(change_question, inputs=[week_file, index, gr.Number(1, visible=False)], outputs=[index, question_output, answer_output])
117
 
118
+ # Chatbot button action
119
+ chat_button.click(chat_with_ai, inputs=[chat_input], outputs=[chat_output])
120
+
121
  # Launch the app
122
  demo.launch()