File size: 6,371 Bytes
ba459e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os
import time
from typing import List, Tuple, Optional

import google.generativeai as genai
import gradio as gr
from PIL import Image

# Ensure Google API Key is set
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")

TITLE = """<h1 align="center">β˜• Espresso with LeProf πŸ”₯</h1>"""
SUBTITLE = """<h2 align="center">🌟 Knowledge Shots for Curious Minds</h2>"""

IMAGE_WIDTH = 512

def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
    return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None

def preprocess_image(image: Image.Image) -> Image.Image:
    image_height = int(image.height * IMAGE_WIDTH / image.width)
    return image.resize((IMAGE_WIDTH, image_height))

def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
    """Handles user input and appends to the chatbot."""
    return "", chatbot + [[text_prompt, None]]

def bot(
    google_key: str,
    image_prompt: Optional[Image.Image],
    temperature: float,
    max_output_tokens: int,
    stop_sequences: str,
    top_k: int,
    top_p: float,
    topic: str,
    chatbot: List[Tuple[str, str]],
):
    """Generates a response using Google Gemini."""
    google_key = google_key or GOOGLE_API_KEY
    if not google_key:
        raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
    if not topic:
        raise ValueError("Topic is not set. Please provide a topic.")

    # Get the user's input
    text_prompt = chatbot[-1][0]

    # Construct the system prompt
    analysis_system_prompt = (
        f"You are an expert in {topic}. Analyze the provided text with a focus on {topic}, "
        "identifying recent issues, insights, or improvements relevant to academic standards and effectiveness. "
        "Offer actionable advice for enhancing knowledge and provide real-life examples."
    )

    # Configure Generative AI model
    genai.configure(api_key=google_key)
    generation_config = genai.types.GenerationConfig(
        temperature=temperature,
        max_output_tokens=max_output_tokens,
        stop_sequences=preprocess_stop_sequences(stop_sequences),
        top_k=top_k,
        top_p=top_p,
    )

    model_name = "gemini-1.5-pro-latest"
    model = genai.GenerativeModel(model_name)

    try:
        # Pass the system prompt and user input as a single prompt
        response = model.generate_content(
            prompt=analysis_system_prompt,
            input=text_prompt,
            generation_config=generation_config,
        )
    except KeyError as e:
        raise KeyError(f"Error in response generation: {e}")

    # Process and stream the response
    chatbot[-1][1] = ""
    for chunk in response:
        for i in range(0, len(chunk.text), 10):
            chatbot[-1][1] += chunk.text[i : i + 10]
            time.sleep(0.01)
            yield chatbot

# Gradio Components
google_key_component = gr.Textbox(
    label="GOOGLE API KEY",
    type="password",
    placeholder="Enter your API key...",
    visible=GOOGLE_API_KEY is None,
)

topic_input = gr.Textbox(label="Set the Topic", placeholder="e.g., AI in Education, Human-Computer Interaction")
text_prompt_component = gr.Textbox(label="Ask LeProf", placeholder="Type your question here...")
chatbot_component = gr.Chatbot(label="LeProf says")
run_button_component = gr.Button("πŸ«— Get Your Knowledge Shot")

example_data = [
    ["AI in Education", "What are the challenges in AI tools for personalized learning?"],
    ["Multimedia Accessibility", "How can multimedia be made more accessible to people with disabilities?"],
    ["Ethical AI", "What are the ethical implications of AI in social media content moderation?"],
    ["Virtual Reality", "How does virtual reality improve skill training in industries?"],
    ["Augmented Reality", "What are the UX challenges in augmented reality for urban navigation?"],
]

# Advanced Settings
temperature_component = gr.Slider(
    minimum=0, maximum=1.0, value=0.4, step=0.05, label="Creativity Level"
)
max_output_tokens_component = gr.Slider(
    minimum=1, maximum=2048, value=1024, step=1, label="Max Tokens"
)
stop_sequences_component = gr.Textbox(label="Stop Sequences", placeholder="e.g., STOP, END")
top_k_component = gr.Slider(
    minimum=1, maximum=40, value=32, step=1, label="Top-K Sampling"
)
top_p_component = gr.Slider(
    minimum=0, maximum=1.0, value=1.0, step=0.01, label="Top-P Sampling"
)

# Layout with Gradio Blocks
with gr.Blocks() as demo:
    gr.HTML(TITLE)
    gr.HTML(SUBTITLE)
    google_key_component.render()
    topic_input.render()
    chatbot_component.render()
    text_prompt_component.render()
    gr.Examples(
        examples=example_data,
        inputs=[topic_input, text_prompt_component],
        label="Example Questions",
    )
    run_button_component.render()
    with gr.Accordion("Parameters", open=False):
        temperature_component.render()
        max_output_tokens_component.render()
        stop_sequences_component.render()
        with gr.Accordion("Advanced Settings", open=False):
            top_k_component.render()
            top_p_component.render()

    # Event Handlers
    run_button_component.click(
        fn=user, inputs=[text_prompt_component, chatbot_component], outputs=[text_prompt_component, chatbot_component], queue=False
    ).then(
        fn=bot,
        inputs=[
            google_key_component,
            None,  # Placeholder for image_prompt (not used in this example)
            temperature_component,
            max_output_tokens_component,
            stop_sequences_component,
            top_k_component,
            top_p_component,
            topic_input,
            chatbot_component,
        ],
        outputs=[chatbot_component]
    )
    text_prompt_component.submit(
        fn=user, inputs=[text_prompt_component, chatbot_component], outputs=[text_prompt_component, chatbot_component], queue=False
    ).then(
        fn=bot,
        inputs=[
            google_key_component,
            None,  # Placeholder for image_prompt (not used in this example)
            temperature_component,
            max_output_tokens_component,
            stop_sequences_component,
            top_k_component,
            top_p_component,
            topic_input,
            chatbot_component,
        ],
        outputs=[chatbot_component]
    )

# Launch the App
demo.launch()