Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import standard libraries
|
2 |
+
import os
|
3 |
+
import re
|
4 |
+
import json
|
5 |
+
import getpass
|
6 |
+
import logging
|
7 |
+
|
8 |
+
# Import third-party libraries for web scraping, API interactions, and data processing
|
9 |
+
import requests
|
10 |
+
import pandas as pd
|
11 |
+
from bs4 import BeautifulSoup
|
12 |
+
|
13 |
+
# Import libraries for interacting with OpenAI and other language models
|
14 |
+
import openai
|
15 |
+
import llama_index
|
16 |
+
from llama_index.llms import OpenAI
|
17 |
+
from llama_index.embeddings import OpenAIEmbedding
|
18 |
+
from llama_index.llms import (
|
19 |
+
CustomLLM,
|
20 |
+
CompletionResponse,
|
21 |
+
CompletionResponseGen,
|
22 |
+
LLMMetadata,
|
23 |
+
)
|
24 |
+
|
25 |
+
# Import for creating web interfaces
|
26 |
+
import gradio as gr
|
27 |
+
|
28 |
+
# Import specific utilities for news feed parsing and query processing
|
29 |
+
from RAG_utils import NewsFeedParser, HybridRetriever, NewsQueryEngine
|
30 |
+
|
31 |
+
with open('config.json') as config_file:
|
32 |
+
config = json.load(config_file)
|
33 |
+
|
34 |
+
# Setup logging
|
35 |
+
logging.basicConfig(level=logging.INFO)
|
36 |
+
openai.api_key = config['OPENAI_API_KEY']
|
37 |
+
os.environ['OPENAI_API_KEY'] = openai.api_key
|
38 |
+
|
39 |
+
|
40 |
+
llm = OpenAI(model="gpt-4", temperature=0.1, max_tokens=512)
|
41 |
+
embed_model = OpenAIEmbedding()
|
42 |
+
|
43 |
+
|
44 |
+
def chatbot(input_text):
|
45 |
+
# Create an instance of NewsFeedParser and process query
|
46 |
+
news_parser = NewsFeedParser()
|
47 |
+
documents = news_parser.process_and_chunk_articles(input_text)
|
48 |
+
|
49 |
+
# Initialize the query engine with the processed documents
|
50 |
+
pdf_query_engine = NewsQueryEngine(documents, llm, embed_model)
|
51 |
+
query_engine = pdf_query_engine.setup_query_engine()
|
52 |
+
|
53 |
+
# Process the query using the query engine
|
54 |
+
response = query_engine.query(input_text)
|
55 |
+
return response
|
56 |
+
|
57 |
+
# Gradio interface setup
|
58 |
+
iface = gr.Interface(
|
59 |
+
fn=chatbot,
|
60 |
+
inputs=gr.components.Textbox(lines=3, label="Enter your text:"),
|
61 |
+
outputs=gr.components.Textbox(lines=20, label="Answer:"),
|
62 |
+
title="FinWise Explorer"
|
63 |
+
)
|
64 |
+
|
65 |
+
# Launch the Gradio interface
|
66 |
+
iface.launch(share=True)
|