Spaces:
Sleeping
Sleeping
Commit ·
7632dfc
1
Parent(s): 8c26b41
added citation in UI
Browse files- .gitignore +1 -0
- app.py +61 -59
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
app.py
CHANGED
|
@@ -1,72 +1,74 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
-
messages.extend(history)
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
-
|
| 25 |
-
response = ""
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
)
|
| 62 |
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
-
with gr.Sidebar():
|
| 65 |
-
|
| 66 |
-
gr.
|
| 67 |
-
|
| 68 |
-
# chatbot.render()
|
| 69 |
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
# from huggingface_hub import InferenceClient
|
| 3 |
+
import requests
|
| 4 |
+
from rich.pretty import pprint
|
| 5 |
+
from langfuse import Langfuse
|
| 6 |
+
from langfuse import observe
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
load_dotenv()
|
| 10 |
|
| 11 |
+
@observe()
|
| 12 |
+
def respond(message, history):
|
| 13 |
+
url = "http://ec2-52-202-99-138.compute-1.amazonaws.com/api/v1/agents/6534b3ac82fa11f0b0019acfa5a3a049/completions"
|
| 14 |
+
headers = {
|
| 15 |
+
"Authorization": os.getenv("RAGFLOW_API_KEY"),
|
| 16 |
+
"Content-Type": "application/json"
|
| 17 |
+
}
|
| 18 |
+
payload = {
|
| 19 |
+
"question": message,
|
| 20 |
+
"stream": False,
|
| 21 |
+
}
|
| 22 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 23 |
+
if response.status_code == 200:
|
| 24 |
+
agent_response = response.json()
|
| 25 |
+
pprint(agent_response)
|
| 26 |
+
citations = []
|
| 27 |
+
if agent_response["data"]["data"]["reference"]["chunks"]:
|
| 28 |
+
for id, chunk in agent_response["data"]["data"]["reference"]["chunks"].items():
|
| 29 |
+
citations.append({"cite_id":id,
|
| 30 |
+
"content": chunk['content'],
|
| 31 |
+
"id": chunk["id"],
|
| 32 |
+
"document_name": chunk["document_name"],
|
| 33 |
+
"similarity": chunk["similarity"]})
|
| 34 |
|
| 35 |
+
|
| 36 |
+
history.append({"role": "assistant", "content": agent_response["data"]["data"]["content"]})
|
| 37 |
+
|
| 38 |
+
if citations:
|
| 39 |
+
for cite in citations:
|
| 40 |
+
history.append({
|
| 41 |
+
"role": "assistant",
|
| 42 |
+
"content": (f"{cite['content']}"),
|
| 43 |
+
"metadata": {"title": f"📚 Citation {cite['cite_id']}",
|
| 44 |
+
# "id": cite["id"],
|
| 45 |
+
# "parent_id": cite["document_name"],
|
| 46 |
+
"log": f" (Match: {100*cite['similarity']:.2f}%)"
|
| 47 |
+
}
|
| 48 |
+
})
|
| 49 |
+
return history
|
| 50 |
+
else:
|
| 51 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
chatbot = gr.ChatInterface(
|
| 54 |
respond,
|
| 55 |
type="messages",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
+
# with gr.Sidebar():
|
| 60 |
+
# gr.LoginButton()
|
| 61 |
+
gr.Markdown("# UNICEF EYSN Agent")
|
| 62 |
+
chatbot.render()
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
+
|
| 67 |
+
langfuse = Langfuse(
|
| 68 |
+
secret_key=os.getenv("LANGFUSE_SECRET_KEY"),
|
| 69 |
+
public_key=os.getenv("LANGFUSE_PUBLIC_KEY"),
|
| 70 |
+
host="https://cloud.langfuse.com"
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
langfuse.track_event("demo.launch")
|
| 74 |
+
demo.launch(debug=True)
|