File size: 6,665 Bytes
f7de3bd
60fa7c5
f51a935
8778419
 
3100db4
2c4a0df
 
 
 
 
 
302f7cc
c1b27d1
 
 
 
 
 
 
 
 
 
 
8778419
f5a8f82
8778419
 
 
302f7cc
67602dc
 
 
 
 
 
302f7cc
 
60fa7c5
67602dc
302f7cc
67602dc
 
 
 
302f7cc
67602dc
302f7cc
5162633
40d0abc
8778419
 
 
 
5354d75
8778419
 
40d0abc
8778419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3f19c8
fb95996
8778419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5233d81
8778419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c73aebd
8778419
 
 
 
 
 
 
 
 
5233d81
8778419
c3f19c8
c48afa4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8778419
c48afa4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8778419
75202cd
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
import gradio as gr
from huggingface_hub import InferenceClient
from sentence_transformers import SentenceTransformer
import torch
import numpy as np 
client = InferenceClient('Qwen/Qwen2.5-72B-Instruct')
# Define the theme for the app.
theme_citrus = gr.themes.Citrus(
    primary_hue="green",
    secondary_hue="emerald",
    neutral_hue="lime",
)

#sustainability tips 
SUSTAINABILITY_TIPS = [
    "Bring your own: Carry a reusable water bottle, coffee cup, and shopping bag.",
    "Switch to LEDs: They use less energy and last longer than traditional bulbs.",
    "Reduce food waste: Plan meals, store food properly, and compost scraps.",
    "Choose sustainable transport: Walk, bike, carpool, or take public transit when possible.",
    "Buy less, choose quality: Opt for durable items over disposable ones.",
    "Unplug devices: Save energy by unplugging electronics when not in use.",
    "Go paperless: Switch to digital bills, receipts, and notes.",
    "Support eco-conscious brands: Choose companies with ethical sourcing and minimal packaging."
]
# Open the ECOsphere.txt file in read mode with UTF-8 encoding
with open("eskb.txt", "r", encoding="utf-8") as file:
  # Read the entire contents of the file and store it in a variable
  ECOsphere_text = file.read()

def respond(message, history):
    top_results = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
    print("Top results:", top_results)

    messages = [
        {"role": "system", "content": f"You are a chatbot that encourages people to live more sustainably. Base your response on: {top_results}"}
    ]
    if history:
        messages.extend(history)
    messages.append({"role": "user", "content": message})

    response = client.chat_completion(
        model="Qwen/Qwen2.5-7B-Instruct",
        messages=messages,
        max_tokens=200,
        temperature=0.5
    )
    return response.choices[0].message["content"].strip()


cleaned_chunks = []
def preprocess_text(text):
  # Strip extra whitespace from the beginning and the end of the text
  cleaned_text = text.strip()

  chunks = cleaned_text.split('*')

  # Create an empty list to store cleaned chunks
  #cleaned_chunks = []

  # Write your for-in loop below to clean each chunk and add it to the cleaned_chunks list
  for chunk in chunks:
    stripped_chunk = chunk.strip()
    cleaned_chunks.append(stripped_chunk)


  # Print cleaned_chunks
  print(cleaned_chunks)

  # Print the length of cleaned_chunks
  print(len(cleaned_chunks))

  # Return the cleaned_chunks
  return cleaned_chunks
cleaned_chunks = preprocess_text(ECOsphere_text)
cleaned_chunks.extend(SUSTAINABILITY_TIPS)
# Load the pre-trained embedding model that converts text to vectors
model = SentenceTransformer('all-MiniLM-L6-v2')

def create_embeddings(text_chunks):
  # Convert each text chunk into a vector embedding and store as a tensor
  chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) # Replace ... with the text_chunks list

  # Print the chunk embeddings
  print(chunk_embeddings)

  # Print the shape of chunk_embeddings
  print(chunk_embeddings.shape)

  # Return the chunk_embeddings
  return chunk_embeddings
chunk_embeddings = create_embeddings(cleaned_chunks)
# Define a function to find the most relevant text chunks for a given query, chunk_embeddings, and text_chunks
def get_top_chunks(query, chunk_embeddings, text_chunks):
  # Convert the query text into a vector embedding
  query_embedding = model.encode(query, convert_to_tensor=True) # Complete this line

  # Normalize the query embedding to unit length for accurate similarity comparison
  query_embedding_normalized = query_embedding / query_embedding.norm()

  # Normalize all chunk embeddings to unit length for consistent comparison
  chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)

  # Calculate cosine similarity between query and all chunks using matrix multiplication
  similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized ) # Complete this line

  # Print the similarities
  print(similarities)

  # Find the indices of the 3 chunks with highest similarity scores
  top_indices = torch.topk(similarities, k=5).indices

  # Print the top indices
  print(top_indices)

  # Create an empty list to store the most relevant chunks
  top_chunks = []

  # Loop through the top indices and retrieve the corresponding text chunks
  for indices in top_indices:
    relevant_info = cleaned_chunks[indices]
    top_chunks.append(relevant_info)
  return top_chunks
# Define the function to change the font based on the dropdown selection.
def change_font(font):
    if font == "Open dyslexic":
        gr.HTML('''
        <style>
        @fontface { 
  font-family: openDyslexic;
  src: url(Open_Dyslexic-Regular.otf);
}
body {
  font-family: openDyslexic;
}
      <style/>      ''')
    else:
        font_family = "Arial"  
    return 
# Define the function to handle chatbot responses and maintain history.
def echo_bot(user_input, history):
    history.append((user_input, user_input))
    return "", history

# Create the Gradio app.
with gr.Blocks(theme=theme_citrus) as demo:
   
    
    with gr.Row():
        with gr.Column(scale=1):
            # Dropdown menu for font selection.
            font_dropdown = gr.Dropdown(choices=["Open dyslexic", "Normal"], label="Select Font")
            apply_button = gr.Button("Apply Font")
            text_output = gr.Markdown("Sample text here")
            apply_button.click(fn=change_font, inputs=font_dropdown, outputs=text_output)
        with gr.Column(scale=19):
            # Image component.
            gr.Image('ecosphere.png')
    # Projects section.
    with gr.Row(scale=9):
        with gr.Column(scale=3):
           with gr.Group():
                gr.Markdown('GET INVOLVED')
                with gr.Group():
                    gr.HTML('<a href="https://fridaysforfuture.org/" target="_blank">FRIDAYS FOR FUTURE</a>')
                    gr.HTML('<a href="https://www.sunrisemovement.org/" target="_blank">SUNRISE MOVEMENT</a>')
                    gr.HTML('<a href="https://thisiszerohour.org/" target="_blank">ZERO HOUR</a>')
                    gr.Markdown('Outside the US')
                    gr.HTML('<a href="https://community.youth4climate.info/homepage" target="_blank">Youth4Climate (EU)</a>')
                    gr.HTML('<a href="https://www.instagram.com/fridaysforfuturemapa/?hl=de" target="_blank">Fridays for Future MAPA  (Africa, Latin America, Asia-Pacific) </a>')
        with gr.Column(scale=7):
            gr.ChatInterface(respond, type="messages")            

demo.launch()