File size: 1,271 Bytes
1a68500
6e95f9f
1a68500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import gradio as gr
import os
import google.generativeai as genai
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")

# Now you can use hugging_face_api_key in your code

genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel('gemini-pro')  # Load the model

def get_Answer(query):
    res = collection.query(  # Assuming `collection` is defined elsewhere
        query_texts=query,
        n_results=2
    )
    system = f"""You are a teacher. You will be provided some context, 
    your task is to analyze the relevant context and answer the below question:
    - {query}
    """
    context = " ".join([re.sub(r'[^\x00-\x7F]+', ' ', r) for r in res['documents'][0]])
    prompt = f"### System: {system} \n\n ###: User: {context} \n\n ### Assistant:\n"
    answer = model.generate_content(prompt).text
    return answer

# Define the Gradio interface
iface = gr.Interface(
    fn=get_Answer,
    inputs=gr.Textbox(lines=5, placeholder="Ask a question"),  # Textbox for query
    outputs="textbox",  # Display the generated answer in a textbox
    title="Answer Questions with Gemini-Pro",
    description="Ask a question and get an answer based on context from a ChromaDB collection.",
)

# Launch the Gradio app
iface.launch(debug=True,share=True)