Spaces:
Sleeping
Sleeping
Commit ·
02f5148
1
Parent(s): 9369fd6
initial gradio integration
Browse files- README.md +0 -2
- app.py +42 -60
- requirements.txt +8 -1
README.md
CHANGED
|
@@ -9,5 +9,3 @@ app_file: app.py
|
|
| 9 |
pinned: false
|
| 10 |
short_description: talk about DS9 with Jammer!
|
| 11 |
---
|
| 12 |
-
|
| 13 |
-
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
|
|
|
| 9 |
pinned: false
|
| 10 |
short_description: talk about DS9 with Jammer!
|
| 11 |
---
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,64 +1,46 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from groq import Groq
|
| 3 |
+
from pinecone import Pinecone
|
| 4 |
+
from langchain_pinecone import PineconeVectorStore
|
| 5 |
+
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
import gradio as gr
|
| 8 |
|
| 9 |
+
client = Groq(api_key = os.getenv('GROQ_API_KEY'))
|
| 10 |
+
pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY"))
|
| 11 |
+
index = pc.Index('ds9-documents')
|
| 12 |
+
docsearch = PineconeVectorStore(index=index,
|
| 13 |
+
embedding=SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2"))
|
| 14 |
+
|
| 15 |
+
def transcript_chat_completion(user_question, history):
|
| 16 |
+
|
| 17 |
+
relevent_docs = docsearch.similarity_search(user_question)
|
| 18 |
+
delimiter = '\n\n------------------------------------------------------\n\n'
|
| 19 |
+
num_docs = 12
|
| 20 |
+
relevant_transcripts = delimiter.join([doc.page_content for doc in relevent_docs[:num_docs]])
|
| 21 |
+
|
| 22 |
+
chat_completion = client.chat.completions.create(
|
| 23 |
+
messages=[
|
| 24 |
+
{
|
| 25 |
+
"role": "system",
|
| 26 |
+
"content": '''Use this transcript or transcripts to answer any user questions, citing specific quotes:
|
| 27 |
+
|
| 28 |
+
{transcript}
|
| 29 |
+
'''.format(transcript=relevant_transcripts)
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"role": "user",
|
| 33 |
+
"content": user_question,
|
| 34 |
+
}
|
| 35 |
+
],
|
| 36 |
+
model="llama3-8b-8192",
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
return chat_completion.choices[0].message.content
|
| 40 |
+
|
| 41 |
+
gr.ChatInterface(
|
| 42 |
+
fn=transcript_chat_completion,
|
| 43 |
+
type="messages"
|
| 44 |
+
).launch()
|
| 45 |
|
| 46 |
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1 +1,8 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
beautifulsoup4
|
| 2 |
+
gradio==5.25.0
|
| 3 |
+
groq==0.19.0
|
| 4 |
+
langchain==0.3.23
|
| 5 |
+
langchain-community==0.3.21
|
| 6 |
+
langchain-pinecone==0.2.5
|
| 7 |
+
pandas==2.2.3
|
| 8 |
+
pinecone==6.0.2
|