Spaces:
Sleeping
Sleeping
gautamethiraj
commited on
Commit
•
c89e432
1
Parent(s):
428116e
project files
Browse files- .gitattributes +1 -0
- .gitignore +1 -0
- app.py +41 -3
- metropolen.db +3 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
metropolen.db filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
__pycache__/
|
app.py
CHANGED
@@ -1,7 +1,45 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import sqlite_utils
|
3 |
+
import llm
|
4 |
+
import pandas as pd
|
5 |
+
from openai import OpenAI
|
6 |
+
import os
|
7 |
|
8 |
+
if not os.getenv("OPENAI_API_KEY"):
|
9 |
+
raise ValueError("OPENAI_API_KEY must be set")
|
10 |
|
11 |
+
def retrieval(question: str):
|
12 |
+
db = sqlite_utils.Database("metropolen.db")
|
13 |
+
embedding_model = llm.get_embedding_model("ada-002")
|
14 |
+
collection = llm.Collection("sections", db, model=embedding_model)
|
15 |
+
|
16 |
+
data = [(entry.id, entry.content, entry.score) for entry in collection.similar(question, number=3)]
|
17 |
+
df = pd.DataFrame(data, columns=['section_heading', 'content', 'score'])
|
18 |
+
documents = (df.content).str.cat(sep='\n')
|
19 |
+
|
20 |
+
return documents
|
21 |
+
|
22 |
+
def make_prompt(documents, question):
|
23 |
+
prompt = f"""Given the context below, answer the question at the end of the text.
|
24 |
+
---
|
25 |
+
{documents}
|
26 |
+
---
|
27 |
+
{question}
|
28 |
+
"""
|
29 |
+
return prompt
|
30 |
+
|
31 |
+
def generation(prompt, api_key=None, model="gpt-4o"):
|
32 |
+
api_key = api_key if api_key else os.getenv('OPENAI_API_KEY')
|
33 |
+
client = OpenAI(api_key=api_key)
|
34 |
+
c = client.chat.completions.create(model=model,
|
35 |
+
messages=[{"role": "user", "content": prompt}])
|
36 |
+
return c.choices[0].message.content
|
37 |
+
|
38 |
+
def rag(frage: str) -> str:
|
39 |
+
documents = retrieval(question=frage)
|
40 |
+
prompt = make_prompt(documents=documents, question=frage)
|
41 |
+
antwort = generation(prompt=prompt, api_key=os.getenv('OPENAI_API_KEY'), model="gpt-4o")
|
42 |
+
return antwort
|
43 |
+
|
44 |
+
demo = gr.Interface(fn=rag, inputs="text", outputs="text")
|
45 |
demo.launch()
|
metropolen.db
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:60849cd41fd3ccc9aa626e37f00b450dcab9275c3b5593b06583ef3d12732080
|
3 |
+
size 323584
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
sqlite_utils
|
2 |
+
llm
|
3 |
+
pandas
|
4 |
+
openai
|