File size: 1,286 Bytes
5ec0408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
import gradio as gr
from langchain import FAISS
from langchain.embeddings import OpenAIEmbeddings
import os


embeddings = OpenAIEmbeddings()
faiss_path = Path(__file__).parent / "faiss_index_03"
docsearch = FAISS.load_local(faiss_path, embeddings)

def generate_outputs(query: str, k: int):
    outputs = []
    docs_and_scores = docsearch.similarity_search_with_score(query, k=k)
    for doc, score in docs_and_scores:
        output_text = f"{doc.page_content} + \n Score: {score:.2f}"
        outputs.append(output_text)
    return "\n---------------------------------------------------------\n" \
           "---------------------------------------------------------\n" \
           "---------------------------------------------------------\n".join(outputs)

# Define input/output interfaces and options
iface = gr.Interface(
    fn=generate_outputs,
    inputs=[
        gr.Textbox(label="Enter your text", value="Sample text", lines=2),
        gr.Slider(label="Number of outputs", minimum=1, maximum=12, value=4)
    ],
    outputs=[
         gr.Textbox(label="Generated Outputs")
    ],
    title="Text Generation App",
    description="Enter your text and choose the number of outputs you'd like to generate"
)

# Launch the Gradio app
iface.launch()