File size: 2,288 Bytes
ea31fb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load the Llama 3.2 model and tokenizer
model_name = "your-llama-3-2-model"  # Replace with the correct model path in Hugging Face Hub
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

def search_api_call(query: str):
    api_key = '614e992d87c496fb15a81a2039e00e6a42530f5c'  # Replace with your actual API key
    url = f"https://api.serper.dev/search?api_key={api_key}&q={query}"
    
    try:
        response = requests.get(url)
        data = response.json()
        return data['organic_results']  # Adjust according to the API response structure
    except Exception as e:
        st.error("Error fetching from the API: " + str(e))
        return None

def is_llm_insufficient(llm_response: str) -> bool:
    return "I don't know" in llm_response or llm_response.strip() == ''

def summarize_search_results(results):
    summary = ""
    for result in results:
        summary += f"- {result['title']} ({result['link']})\n"
    return summary

def generate_llm_response(user_query: str) -> str:
    inputs = tokenizer(user_query, return_tensors="pt")
    outputs = model.generate(**inputs, max_length=150)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# Streamlit user interface
st.title("Real-Time Factual Information Fetcher")
user_query = st.text_input("Enter your query:")

if st.button("Submit"):
    if user_query:
        llm_response = generate_llm_response(user_query)

        if is_llm_insufficient(llm_response) or 'recent' in user_query.lower():
            search_results = search_api_call(user_query)
            if search_results:
                search_summary = summarize_search_results(search_results)
                combined_response = f"{llm_response}\n\nHere are some recent findings:\n{search_summary.strip()}"
            else:
                combined_response = llm_response  # Fallback to the original LLM response
        else:
            combined_response = llm_response
        
        st.markdown("### Response:")
        st.markdown(combined_response)

    else:
        st.warning("Please enter a query.")

if __name__ == "__main__":
    import streamlit as st