Spaces:
Sleeping
Sleeping
File size: 3,723 Bytes
2cb5c51 | 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 | from retrival import retrival
from indexing import indexing
import os
from dotenv import load_dotenv
import gradio as gr
from groq import Groq
import uuid
load_dotenv()
client=Groq(api_key=os.getenv("GROQ_API_KEY"))
current_file = None
def handle_file(file, session_id, progress=gr.Progress()):
global current_file
if file is not None:
if current_file != file.name:
status = indexing(file, session_id)
current_file = file.name
return status
return "Already indexed."
def mainwork(query,history,session_id):
if history is None:
history=[]
contextchunks=retrival(query,session_id)
context="\n\n".join([
f"[Source {i}: {doc.metadata.get('source','?')}, page {doc.metadata.get('page','?')}]\n{doc.page_content}"
for i, doc in enumerate(contextchunks, 1)
])
messages = [
{
"role": "system",
"content": """
You are DocBuddy, a helpful document assistant.
Use the retrieved document context as the primary source of truth.
Use the conversation history to understand follow-up questions,
references, and pronouns such as:
- it
- this
- that
- these
- these two
- the previous concept
When answering:
1. Prefer information from the retrieved documents.
2. Combine information from multiple retrieved chunks when needed.
3. Explain concepts clearly and naturally.
4. If the answer cannot be supported by the retrieved documents, say:
'I don't have enough information in the uploaded documents to answer that.'
5. Do not invent facts or citations.
Always end with:
Sources:
- source name and page number
"""
}]
for msg in history:
messages.append({
"role":msg["role"],
"content":msg["content"]
})
messages.append({"role": "user", "content": f"Context:\n{context}\n\nQuestion: {query}"})
response=client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=messages,
stream=True,
temperature=0
)
history.append(
{
"role":"user",
"content":query
})
history.append(
{
"role":"assistant",
"content":""
}
)
for chunk in response:
content=chunk.choices[0].delta.content or ""
history[-1]["content"]+=content
yield history
with gr.Blocks() as demo:
session_id=gr.State(value=lambda:str(uuid.uuid4()))
with gr.Tab("💬 Chat"):
with gr.Column(scale=3):
chatbot=gr.Chatbot(height=440,label="Conversation")
textbox=gr.Textbox(placeholder="Ask anything here.....",show_label=False)
button=gr.Button("Send")
textbox.submit(
mainwork,
inputs=[textbox,chatbot,session_id],
outputs=[chatbot]
).then(lambda:(""),outputs=textbox)
button.click(
mainwork,
inputs=[textbox,chatbot,session_id],
outputs=[chatbot]
).then(lambda: (""),outputs=[textbox])
with gr.Tab("Documents"):
gr.Markdown("Give your documents here")
file=gr.File()
indx_status=gr.Textbox("Indexing status",interactive=False)
file.change(
handle_file,
inputs=[file,session_id],
outputs=[indx_status]
).then(lambda: None,outputs=[file])
demo.launch()
|