Spaces:
Runtime error
Runtime error
academy04 commited on
Commit ·
40d0295
1
Parent(s): ee7a0fe
add db
Browse files- app.py +47 -15
- requirements.txt +5 -1
- test_script/import_data_to_rag.py +21 -0
- test_script/summarize_text.py +33 -0
app.py
CHANGED
|
@@ -1,22 +1,66 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
| 4 |
client = OpenAI(
|
| 5 |
base_url="https://openrouter.ai/api/v1",
|
| 6 |
-
api_key="
|
| 7 |
)
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
| 12 |
history: list[tuple[str, str]],
|
| 13 |
):
|
| 14 |
response = ""
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
response = client.chat.completions.create(
|
| 17 |
extra_body={},
|
| 18 |
model="openrouter/optimus-alpha",
|
| 19 |
messages=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
{
|
| 21 |
"role": "user",
|
| 22 |
"content": [
|
|
@@ -35,19 +79,7 @@ def respond(
|
|
| 35 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 36 |
"""
|
| 37 |
demo = gr.ChatInterface(
|
| 38 |
-
respond,
|
| 39 |
-
# additional_inputs=[
|
| 40 |
-
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 41 |
-
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 42 |
-
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 43 |
-
# gr.Slider(
|
| 44 |
-
# minimum=0.1,
|
| 45 |
-
# maximum=1.0,
|
| 46 |
-
# value=0.95,
|
| 47 |
-
# step=0.05,
|
| 48 |
-
# label="Top-p (nucleus sampling)",
|
| 49 |
-
# ),
|
| 50 |
-
# ],
|
| 51 |
)
|
| 52 |
|
| 53 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
+
import os
|
| 4 |
+
import chromadb
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
|
| 7 |
+
ds = load_dataset("RTVS/SpotifyLyrics001")[:500]
|
| 8 |
+
apikey=os.getenv('apikey')
|
| 9 |
client = OpenAI(
|
| 10 |
base_url="https://openrouter.ai/api/v1",
|
| 11 |
+
api_key=f"{apikey}",
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# Hàm chuẩn bị chạy trước Gradio
|
| 15 |
+
def setup_chromadb():
|
| 16 |
+
global collection # để dùng lại trong các hàm Gradio nếu cần
|
| 17 |
+
client = chromadb.Client()
|
| 18 |
+
collection_name = "my_collection"
|
| 19 |
+
|
| 20 |
+
existing_collections = [col.name for col in client.list_collections()]
|
| 21 |
+
if collection_name not in existing_collections:
|
| 22 |
+
print(f"Creating collection: {collection_name}")
|
| 23 |
+
collection = client.create_collection(name=collection_name)
|
| 24 |
+
ids = []
|
| 25 |
+
i = 0
|
| 26 |
+
for song,artist in zip(ds['train']['song'], ds['train']['artist']):
|
| 27 |
+
ids.append(f"Index: {i} - Name song:{song} - Artis:{artist}")
|
| 28 |
+
i+=1
|
| 29 |
+
collection.add(
|
| 30 |
+
documents=ds['train']['text'],
|
| 31 |
+
ids=ids
|
| 32 |
+
)
|
| 33 |
+
else:
|
| 34 |
+
print(f"Collection {collection_name} already exists.")
|
| 35 |
+
collection = client.get_collection(name=collection_name)
|
| 36 |
+
|
| 37 |
+
return
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
setup_chromadb()
|
| 41 |
|
| 42 |
def respond(
|
| 43 |
message,
|
| 44 |
history: list[tuple[str, str]],
|
| 45 |
):
|
| 46 |
response = ""
|
| 47 |
+
musics = collection.query(
|
| 48 |
+
query_texts=[message],
|
| 49 |
+
n_results=10
|
| 50 |
+
)['ids'][0]
|
| 51 |
response = client.chat.completions.create(
|
| 52 |
extra_body={},
|
| 53 |
model="openrouter/optimus-alpha",
|
| 54 |
messages=[
|
| 55 |
+
{
|
| 56 |
+
"role": "system",
|
| 57 |
+
"content": [
|
| 58 |
+
{
|
| 59 |
+
"type": "text",
|
| 60 |
+
"text": f"Generate a recommendation for the song based on user and this list: {musics}"
|
| 61 |
+
},
|
| 62 |
+
]
|
| 63 |
+
},
|
| 64 |
{
|
| 65 |
"role": "user",
|
| 66 |
"content": [
|
|
|
|
| 79 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 80 |
"""
|
| 81 |
demo = gr.ChatInterface(
|
| 82 |
+
fn=respond,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
|
requirements.txt
CHANGED
|
@@ -1 +1,5 @@
|
|
| 1 |
-
openai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
huggingface_hub
|
| 3 |
+
datasets
|
| 4 |
+
chromadb
|
| 5 |
+
sentence_transformers
|
test_script/import_data_to_rag.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import chromadb
|
| 2 |
+
|
| 3 |
+
chroma_client = chromadb.Client()
|
| 4 |
+
|
| 5 |
+
collection = chroma_client.create_collection(name="SpotifyLyrics")
|
| 6 |
+
|
| 7 |
+
collection.add(
|
| 8 |
+
documents=[
|
| 9 |
+
"This is a document about pineapple",
|
| 10 |
+
"This is a document about oranges",
|
| 11 |
+
"This is a query document about hawaii"
|
| 12 |
+
],
|
| 13 |
+
ids=["id1", "id2", "id3"],
|
| 14 |
+
)
|
| 15 |
+
query_texts=["fruit"]
|
| 16 |
+
|
| 17 |
+
results = collection.query(
|
| 18 |
+
query_texts=query_texts,
|
| 19 |
+
n_results=2
|
| 20 |
+
)
|
| 21 |
+
print(results)
|
test_script/summarize_text.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import load_dataset
|
| 2 |
+
|
| 3 |
+
ds = load_dataset("RTVS/SpotifyLyrics001")
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
|
| 6 |
+
client = OpenAI(
|
| 7 |
+
base_url="https://openrouter.ai/api/v1",
|
| 8 |
+
api_key="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",
|
| 9 |
+
)
|
| 10 |
+
summarized_lyrics = []
|
| 11 |
+
|
| 12 |
+
for index in range(len(ds['train']['text'])):
|
| 13 |
+
message = ds['train']['text'][index]
|
| 14 |
+
response = client.chat.completions.create(
|
| 15 |
+
extra_body={},
|
| 16 |
+
model="openrouter/optimus-alpha",
|
| 17 |
+
messages=[
|
| 18 |
+
{
|
| 19 |
+
"role": "user",
|
| 20 |
+
"content": [
|
| 21 |
+
{
|
| 22 |
+
"type": "text",
|
| 23 |
+
"text": f"summarize this lyrics of the song: {message}"
|
| 24 |
+
},
|
| 25 |
+
]
|
| 26 |
+
}
|
| 27 |
+
]
|
| 28 |
+
).choices[0].message.content
|
| 29 |
+
print("ok")
|
| 30 |
+
summarized_lyrics.append(response)
|
| 31 |
+
ds = ds.add_column("summarized-lyrics", summarized_lyrics)
|
| 32 |
+
ds.push_to_hub("tqhuyen/Spotify-Lyrics-Summarized")
|
| 33 |
+
|