Spaces:
Runtime error
Runtime error
Commit
·
1a68500
1
Parent(s):
8687413
Create query.py
Browse files
query.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import os
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 5 |
+
|
| 6 |
+
# Now you can use hugging_face_api_key in your code
|
| 7 |
+
|
| 8 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 9 |
+
model = genai.GenerativeModel('gemini-pro') # Load the model
|
| 10 |
+
|
| 11 |
+
def get_Answer(query):
|
| 12 |
+
res = collection.query( # Assuming `collection` is defined elsewhere
|
| 13 |
+
query_texts=query,
|
| 14 |
+
n_results=2
|
| 15 |
+
)
|
| 16 |
+
system = f"""You are a teacher. You will be provided some context,
|
| 17 |
+
your task is to analyze the relevant context and answer the below question:
|
| 18 |
+
- {query}
|
| 19 |
+
"""
|
| 20 |
+
context = " ".join([re.sub(r'[^\x00-\x7F]+', ' ', r) for r in res['documents'][0]])
|
| 21 |
+
prompt = f"### System: {system} \n\n ###: User: {context} \n\n ### Assistant:\n"
|
| 22 |
+
answer = model.generate_content(prompt).text
|
| 23 |
+
return answer
|
| 24 |
+
|
| 25 |
+
# Define the Gradio interface
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=get_Answer,
|
| 28 |
+
inputs=gr.Textbox(lines=5, placeholder="Ask a question"), # Textbox for query
|
| 29 |
+
outputs="textbox", # Display the generated answer in a textbox
|
| 30 |
+
title="Answer Questions with Gemini-Pro",
|
| 31 |
+
description="Ask a question and get an answer based on context from a ChromaDB collection.",
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Launch the Gradio app
|
| 35 |
+
iface.launch(debug=True,share=True)
|