mt3842ml commited on
Commit
262247c
·
verified ·
1 Parent(s): 5a17654

Update app.py

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