mt3842ml commited on
Commit
dd73f68
·
verified ·
1 Parent(s): f1fc0cc

test update

Browse files
Files changed (1) hide show
  1. app.py +39 -89
app.py CHANGED
@@ -1,75 +1,32 @@
1
- import gradio as gr
2
  import os
 
 
 
3
  from groq import Groq
4
- import pandas as pd
5
- from datasets import Dataset
6
  from semantic_router.encoders import HuggingFaceEncoder
7
 
8
  encoder = HuggingFaceEncoder(name="dwzhu/e5-base-4k")
9
-
10
  embeds = encoder(["this is a test"])
11
  dims = len(embeds[0])
12
 
13
- ############ TESTING ############
14
-
15
- import os
16
- import getpass
17
- from pinecone import Pinecone
18
-
19
- # initialize connection to pinecone (get API key at app.pinecone.io)
20
- api_key = os.getenv("PINECONE_API_KEY")
21
-
22
- # configure client
23
- pc = Pinecone(api_key=api_key)
24
-
25
- from pinecone import ServerlessSpec
26
-
27
- spec = ServerlessSpec(
28
- cloud="aws", region="us-east-1"
29
- )
30
-
31
- import time
32
-
33
  index_name = "groq-llama-3-rag"
34
- existing_indexes = [
35
- index_info["name"] for index_info in pc.list_indexes()
36
- ]
37
 
38
- # check if index already exists (it shouldn't if this is first time)
39
- if index_name not in existing_indexes:
40
- # if does not exist, create index
41
- pc.create_index(
42
- index_name,
43
- dimension=dims,
44
- metric='cosine',
45
- spec=spec
46
- )
47
- # wait for index to be initialized
48
  while not pc.describe_index(index_name).status['ready']:
49
  time.sleep(1)
50
 
51
- # connect to index
52
  index = pc.Index(index_name)
53
- time.sleep(1)
54
- # view index stats
55
- index.describe_index_stats()
56
-
57
 
58
  def get_docs(query: str, top_k: int) -> list[str]:
59
- # encode query
60
  xq = encoder([query])
61
- # search pinecone index
62
  res = index.query(vector=xq, top_k=top_k, include_metadata=True)
63
- # get doc text
64
- docs = [x["metadata"]['content_snippet'] for x in res["matches"]]
65
- return docs
66
-
67
- from groq import Groq
68
- groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
69
 
70
  def generate(query: str, history):
71
-
72
- # Create system message
73
  if not history:
74
  system_message = (
75
  "You are a friendly and knowledgeable New Yorker who loves sharing recommendations about the city. "
@@ -77,45 +34,29 @@ def generate(query: str, history):
77
  "Your goal is to give recommendations tailored to what the user is asking for, whether they want iconic attractions "
78
  "or lesser-known spots loved by locals.\n\n"
79
  "Use the provided context to enhance your responses with real local insights, but only include details that are relevant "
80
- "to the user’s question. If the context provides useful recommendations that match what the user is asking for, use them. "
81
- "If the context is unrelated or does not fully answer the question, rely on your general NYC knowledge instead.\n\n"
82
- "Be specific when recommending places—mention neighborhoods, the atmosphere, and why someone might like a spot. "
83
- "Keep your tone warm, conversational, and engaging, like a close friend who genuinely enjoys sharing their city.\n\n"
84
- "CONTEXT:\n"
85
- "\n---\n".join(get_docs(query, top_k=5))
86
  )
87
- messages = [
88
- {"role": "system", "content": system_message},
89
- ]
90
  else:
91
- # Establish history
92
  messages = []
93
  for user_msg, bot_msg in history:
94
  messages.append({"role": "user", "content": user_msg})
95
  messages.append({"role": "assistant", "content": bot_msg})
96
- messages.append({"role": "assistant", "content": bot_msg})
97
- system_message = (
98
- "Here is additional context based on the newest query.\n\n"
99
- "CONTEXT:\n"
100
- "\n---\n".join(get_docs(query, top_k=5))
101
- )
102
- messages.append({"role": "system", "content": system_message})
103
 
104
- # Add query
105
  messages.append({"role": "user", "content": query})
106
-
107
- # generate response
108
- chat_response = groq_client.chat.completions.create(
109
- model="llama3-70b-8192",
110
- messages=messages
111
- )
112
- return chat_response.choices[0].message.content
113
-
114
 
115
- # Custom CSS for iPhone-style chat
116
  custom_css = """
117
  .gradio-container {
118
- background: transparent !important;
 
 
 
 
 
119
  }
120
  .chat-message {
121
  display: flex;
@@ -141,19 +82,28 @@ custom_css = """
141
  border-bottom-right-radius: 5px;
142
  }
143
  .chat-bubble.assistant {
144
- background-color: #f0f0f0;
145
  color: black;
146
  border-bottom-left-radius: 5px;
147
  }
148
- .profile-pic {
149
- width: 40px;
150
- height: 40px;
151
- border-radius: 50%;
152
- margin: 0 10px;
153
- }
154
  """
155
 
156
- # Gradio Interface
157
- demo = gr.ChatInterface(generate, css=custom_css)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
  demo.launch()
 
 
1
  import os
2
+ import time
3
+ import gradio as gr
4
+
5
  from groq import Groq
6
+ from pinecone import Pinecone, ServerlessSpec
 
7
  from semantic_router.encoders import HuggingFaceEncoder
8
 
9
  encoder = HuggingFaceEncoder(name="dwzhu/e5-base-4k")
 
10
  embeds = encoder(["this is a test"])
11
  dims = len(embeds[0])
12
 
13
+ groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
14
+ pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  index_name = "groq-llama-3-rag"
 
 
 
16
 
17
+ if index_name not in [i["name"] for i in pc.list_indexes()]:
18
+ pc.create_index(index_name, dimension=dims, metric='cosine', spec=ServerlessSpec(cloud="aws", region="us-east-1"))
 
 
 
 
 
 
 
 
19
  while not pc.describe_index(index_name).status['ready']:
20
  time.sleep(1)
21
 
 
22
  index = pc.Index(index_name)
 
 
 
 
23
 
24
  def get_docs(query: str, top_k: int) -> list[str]:
 
25
  xq = encoder([query])
 
26
  res = index.query(vector=xq, top_k=top_k, include_metadata=True)
27
+ return [x["metadata"]['content_snippet'] for x in res["matches"]]
 
 
 
 
 
28
 
29
  def generate(query: str, history):
 
 
30
  if not history:
31
  system_message = (
32
  "You are a friendly and knowledgeable New Yorker who loves sharing recommendations about the city. "
 
34
  "Your goal is to give recommendations tailored to what the user is asking for, whether they want iconic attractions "
35
  "or lesser-known spots loved by locals.\n\n"
36
  "Use the provided context to enhance your responses with real local insights, but only include details that are relevant "
37
+ "to the user’s question.\n\n"
38
+ "CONTEXT:\n" + "\n---\n".join(get_docs(query, top_k=5))
 
 
 
 
39
  )
40
+ messages = [{"role": "system", "content": system_message}]
 
 
41
  else:
 
42
  messages = []
43
  for user_msg, bot_msg in history:
44
  messages.append({"role": "user", "content": user_msg})
45
  messages.append({"role": "assistant", "content": bot_msg})
46
+ messages.append({"role": "system", "content": "CONTEXT:\n" + "\n---\n".join(get_docs(query, top_k=5))})
 
 
 
 
 
 
47
 
 
48
  messages.append({"role": "user", "content": query})
49
+ response = groq_client.chat.completions.create(model="llama3-70b-8192", messages=messages)
50
+ return response.choices[0].message.content
 
 
 
 
 
 
51
 
 
52
  custom_css = """
53
  .gradio-container {
54
+ background: #ffffff !important;
55
+ }
56
+ .chatbot {
57
+ background: #f8f8f8 !important;
58
+ border-radius: 15px;
59
+ padding: 10px;
60
  }
61
  .chat-message {
62
  display: flex;
 
82
  border-bottom-right-radius: 5px;
83
  }
84
  .chat-bubble.assistant {
85
+ background-color: #d1d1d6;
86
  color: black;
87
  border-bottom-left-radius: 5px;
88
  }
 
 
 
 
 
 
89
  """
90
 
91
+ with gr.Blocks(css=custom_css) as demo:
92
+ chatbot = gr.Chatbot(label="NYC Buddy", elem_classes=["chatbot"])
93
+ state = gr.State([])
94
+ user_input = gr.Textbox(placeholder="Ask me anything about NYC!")
95
+ send_btn = gr.Button("Send")
96
+
97
+ def respond(message, history):
98
+ response = generate(message, history)
99
+ history = history + [(message, response)]
100
+ return history, ""
101
+
102
+ def startup():
103
+ welcome_msg = "Hey! I'm your NYC Buddy. Looking for local tips, hidden gems, or iconic spots? Just ask!"
104
+ return [(None, welcome_msg)], []
105
+
106
+ demo.load(startup, outputs=[chatbot, state])
107
+ send_btn.click(respond, inputs=[user_input, state], outputs=[chatbot, user_input])
108
 
109
  demo.launch()