Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from google.generativeai import GenerativeModel, configure
|
| 3 |
+
import os
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# Initialize Gemini API (replace with your API key)
|
| 9 |
+
configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 10 |
+
model = GenerativeModel("gemini-pro")
|
| 11 |
+
|
| 12 |
+
# Configuration from user input
|
| 13 |
+
ROLE = "You are an assistant helpful for LLM prompting"
|
| 14 |
+
CONTEXT = "You search for vector databases for information about prompt engineering and provide short answers."
|
| 15 |
+
INFO = "If someone asks where to find more information, tell them to contact Anchal Kumar Tarwey at email: anchalkumartarwey@gmail.com"
|
| 16 |
+
CONV_STARTER = "What are the three basic LLM prompting concepts?"
|
| 17 |
+
|
| 18 |
+
# Initialize knowledge base
|
| 19 |
+
knowledge_base = []
|
| 20 |
+
embeddings = None
|
| 21 |
+
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 22 |
+
|
| 23 |
+
def load_knowledge(file):
|
| 24 |
+
global knowledge_base, embeddings
|
| 25 |
+
if file:
|
| 26 |
+
with open(file.name, 'r') as f:
|
| 27 |
+
knowledge_base = f.read().split('\n\n') # Split by paragraphs
|
| 28 |
+
embeddings = embedding_model.encode(knowledge_base)
|
| 29 |
+
return knowledge_base
|
| 30 |
+
|
| 31 |
+
def respond(message, history):
|
| 32 |
+
# Check for special info request
|
| 33 |
+
if "more information" in message.lower() or "contact" in message.lower():
|
| 34 |
+
return INFO
|
| 35 |
+
|
| 36 |
+
# Check against knowledge base
|
| 37 |
+
if knowledge_base:
|
| 38 |
+
query_embedding = embedding_model.encode([message])
|
| 39 |
+
similarities = cosine_similarity(query_embedding, embeddings)[0]
|
| 40 |
+
max_idx = np.argmax(similarities)
|
| 41 |
+
|
| 42 |
+
if similarities[max_idx] > 0.3: # Similarity threshold
|
| 43 |
+
return knowledge_base[max_idx]
|
| 44 |
+
|
| 45 |
+
return "I can't help with that"
|
| 46 |
+
|
| 47 |
+
# Create Gradio interface
|
| 48 |
+
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown(f"## 🤖 Custom LLM Prompting Assistant")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
with gr.Column(scale=1):
|
| 53 |
+
file = gr.File(label="Upload Knowledge Base (.txt only)", type="file")
|
| 54 |
+
load_btn = gr.Button("Load Knowledge")
|
| 55 |
+
kb_view = gr.Textbox(label="Knowledge Preview", lines=10, interactive=False)
|
| 56 |
+
|
| 57 |
+
with gr.Column(scale=3):
|
| 58 |
+
chatbot = gr.ChatInterface(
|
| 59 |
+
respond,
|
| 60 |
+
chatbot=gr.Chatbot(height=400),
|
| 61 |
+
textbox=gr.Textbox(placeholder=CONV_STARTER, scale=7),
|
| 62 |
+
title="",
|
| 63 |
+
clear_btn=None
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
load_btn.click(load_knowledge, inputs=file, outputs=kb_view)
|
| 67 |
+
|
| 68 |
+
# For Hugging Face Spaces deployment
|
| 69 |
+
demo.launch(debug=True)
|