Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,88 @@ import gradio as gr
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
import openai
|
|
|
|
|
5 |
|
|
|
|
|
6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
-
""
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
def respond(
|
@@ -18,29 +94,8 @@ def respond(
|
|
18 |
temperature,
|
19 |
top_p,
|
20 |
):
|
21 |
-
|
22 |
-
|
23 |
-
for val in history:
|
24 |
-
if val[0]:
|
25 |
-
messages.append({"role": "user", "content": val[0]})
|
26 |
-
if val[1]:
|
27 |
-
messages.append({"role": "assistant", "content": val[1]})
|
28 |
-
|
29 |
-
messages.append({"role": "user", "content": message})
|
30 |
-
|
31 |
-
response = ""
|
32 |
-
|
33 |
-
for message in client.chat_completion(
|
34 |
-
messages,
|
35 |
-
max_tokens=max_tokens,
|
36 |
-
stream=True,
|
37 |
-
temperature=temperature,
|
38 |
-
top_p=top_p,
|
39 |
-
):
|
40 |
-
token = message.choices[0].delta.content
|
41 |
-
|
42 |
-
response += token
|
43 |
-
yield response
|
44 |
|
45 |
"""
|
46 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
import openai
|
5 |
+
import pandas as pd
|
6 |
+
from sentence_transformers import SentenceTransformer
|
7 |
|
8 |
+
|
9 |
+
embedding_model = SentenceTransformer('nomic-ai/nomic-embed-text-v1.5', trust_remote_code=True)
|
10 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
11 |
+
db_index = faiss.read_index("db_index.faiss")
|
12 |
+
metadata_info = pd.read_csv('clean_data.csv')
|
13 |
+
|
14 |
+
|
15 |
+
def search(query):
|
16 |
+
cleaned_query = query
|
17 |
+
query_embedding = embedding_model.encode(cleaned_query).reshape(1, -1).astype('float32')
|
18 |
+
D, I = db_index.search(query_embedding, k=10)
|
19 |
+
results = []
|
20 |
+
for idx in I[0]:
|
21 |
+
if idx < len(metadata_embeddings):
|
22 |
+
doc_index = idx
|
23 |
+
results.append({
|
24 |
+
'type': 'metadata',
|
25 |
+
'title': df.iloc[doc_index]['title'],
|
26 |
+
'author': df.iloc[doc_index]['author'],
|
27 |
+
'publish_date': df.iloc[doc_index]['publish_date'],
|
28 |
+
'full_text': df.iloc[doc_index]['full_text'],
|
29 |
+
'source': df.iloc[doc_index]['url']
|
30 |
+
})
|
31 |
+
else:
|
32 |
+
chunk_index = idx - len(metadata_embeddings)
|
33 |
+
metadata = metadata_info[chunk_index]
|
34 |
+
doc_index = metadata['index']
|
35 |
+
chunk_text = metadata['chunk']
|
36 |
+
results.append({
|
37 |
+
'type': 'content',
|
38 |
+
'title': df.iloc[doc_index]['title'],
|
39 |
+
'author': df.iloc[doc_index]['author'],
|
40 |
+
'publish_date': df.iloc[doc_index]['publish_date'],
|
41 |
+
'content': chunk_text,
|
42 |
+
'source': df.iloc[doc_index]['url']
|
43 |
+
})
|
44 |
+
|
45 |
+
return results
|
46 |
+
|
47 |
+
|
48 |
+
def generate_answer(query):
|
49 |
+
prompt = f"""
|
50 |
+
Based on the following query from a user, please generate a detailed answer based on the context
|
51 |
+
focusing on which is the best based on the query. You should responsd as you are a news and politician expert agent and are conversing with the
|
52 |
+
user in a nice cordial way. If the query question is not in the context say I don't know, and always provide the url as the source of the information.
|
53 |
+
Remove the special characters and (/n ) , make the output clean and concise.
|
54 |
+
|
55 |
+
###########
|
56 |
+
query:
|
57 |
+
"{query}"
|
58 |
+
|
59 |
+
########
|
60 |
+
|
61 |
+
context:"
|
62 |
+
"{search(query)}"
|
63 |
+
#####
|
64 |
+
|
65 |
+
Return in Markdown format with each hotel highlighted.
|
66 |
+
"""
|
67 |
+
|
68 |
+
messages = [
|
69 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
70 |
+
{"role": "user", "content": prompt}
|
71 |
+
]
|
72 |
+
response = openai.ChatCompletion.create(
|
73 |
+
model="gpt-4o-mini",
|
74 |
+
max_tokens=1500,
|
75 |
+
n=1,
|
76 |
+
stop=None,
|
77 |
+
temperature=0.2, #higher temperature means more creative or more hallucination
|
78 |
+
messages = messages
|
79 |
+
|
80 |
+
|
81 |
+
)
|
82 |
+
|
83 |
+
# Extract the generated response from the API response
|
84 |
+
generated_text = response.choices[0].message['content'].strip()
|
85 |
+
|
86 |
+
return generated_text
|
87 |
|
88 |
|
89 |
def respond(
|
|
|
94 |
temperature,
|
95 |
top_p,
|
96 |
):
|
97 |
+
response = generate_answer(message)
|
98 |
+
yield response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
"""
|
101 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|