File size: 2,337 Bytes
7e1dfd1
2bf38d0
7e1dfd1
2bf38d0
 
fe3dd0b
7620448
fe3dd0b
 
 
35a96c0
fe3dd0b
 
80ac8cc
2bf38d0
fe3dd0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bf38d0
fe3dd0b
2bf38d0
 
 
fe3dd0b
 
 
 
 
2bf38d0
80ac8cc
fe3dd0b
 
 
 
80ac8cc
2bf38d0
 
 
 
 
 
 
7e1dfd1
 
fe3dd0b
7e1dfd1
fe3dd0b
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import gradio as gr
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
import openai
import torch
import logging

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Initialize OpenAI API key
openai.api_key = 'YOUR_API_KEY'  # Replace with your API key

def process_query(query):
    try:
        # Log query processing
        logger.info(f"Processing query: {query}")
        
        # Get relevant documents
        relevant_docs = vectordb.similarity_search(query, k=30)
        context = " ".join([doc.page_content for doc in relevant_docs])
        
        # Add delay to respect API rate limits
        time.sleep(1)
        
        # Generate response using OpenAI
        response = openai.chat.completions.create(
            model="gpt-4",
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": f"Given the document: {context}\n\nGenerate a response to the query: {query}"}
            ],
            max_tokens=300,
            temperature=0.7,
        )
        
        answer = response.choices[0].message.content.strip()
        logger.info("Successfully generated response")
        return answer
        
    except Exception as e:
        logger.error(f"Error processing query: {str(e)}")
        return f"Here's what went wrong: {str(e)}"

# Enhanced Gradio interface
demo = gr.Interface(
    fn=process_query,
    inputs=[
        gr.Textbox(
            label="Enter your question",
            placeholder="Type your question here...",
            lines=2
        )
    ],
    outputs=[
        gr.Textbox(
            label="Answer",
            lines=5
        )
    ],
    title="RAG-Powered Question Answering System",
    description="Ask questions and get answers based on the embedded document knowledge.",
    examples=[
        ["What role does T-cell count play in severe human adenovirus type 55 (HAdV-55) infection?"],
        ["In what school district is Governor John R. Rogers High School located?"],
        ["Is there a functional neural correlate of individual differences in cardiovascular reactivity?"]
    ]
)

# Launch with debugging enabled
if __name__ == "__main__":
    demo.launch(debug=True)