Spaces:
Sleeping
Sleeping
fragger246
commited on
Commit
•
285307a
1
Parent(s):
b3e3f7d
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,107 @@
|
|
1 |
-
import os
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
-
from transformers import BertTokenizer, BertModel, GPT2LMHeadModel, GPT2Tokenizer
|
5 |
-
import torch
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
def
|
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 |
-
"How
|
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 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
from transformers import BertTokenizer, BertModel, GPT2LMHeadModel, GPT2Tokenizer
|
5 |
+
import torch
|
6 |
+
from fastapi import FastAPI, Request
|
7 |
+
from pydantic import BaseModel
|
8 |
+
import gradio as gr
|
9 |
+
import uvicorn
|
10 |
+
|
11 |
+
# Initialize FastAPI
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
# Initialize the BERT model and tokenizer
|
15 |
+
bert_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
16 |
+
bert_model = BertModel.from_pretrained('bert-base-uncased')
|
17 |
+
|
18 |
+
def get_bert_embeddings(texts):
|
19 |
+
inputs = bert_tokenizer(texts, return_tensors='pt', padding=True, truncation=True)
|
20 |
+
with torch.no_grad():
|
21 |
+
outputs = bert_model(**inputs)
|
22 |
+
return outputs.last_hidden_state[:, 0, :].numpy()
|
23 |
+
|
24 |
+
def get_closest_question(user_query, questions, threshold=0.95):
|
25 |
+
all_texts = questions + [user_query]
|
26 |
+
embeddings = get_bert_embeddings(all_texts)
|
27 |
+
cosine_similarities = np.dot(embeddings[-1], embeddings[:-1].T) / (
|
28 |
+
np.linalg.norm(embeddings[-1]) * np.linalg.norm(embeddings[:-1], axis=1)
|
29 |
+
)
|
30 |
+
max_similarity = np.max(cosine_similarities)
|
31 |
+
if max_similarity >= threshold:
|
32 |
+
most_similar_index = np.argmax(cosine_similarities)
|
33 |
+
return questions[most_similar_index], max_similarity
|
34 |
+
else:
|
35 |
+
return None, max_similarity
|
36 |
+
|
37 |
+
def generate_gpt2_response(prompt, model, tokenizer, max_length=100):
|
38 |
+
inputs = tokenizer.encode(prompt, return_tensors='pt')
|
39 |
+
outputs = model.generate(inputs, max_length=max_length, num_return_sequences=1)
|
40 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
41 |
+
|
42 |
+
# Initialize data
|
43 |
+
data_dict = {
|
44 |
+
"questions": [
|
45 |
+
"What is Rookus?",
|
46 |
+
"How does Rookus use AI in its designs?",
|
47 |
+
"What products does Rookus offer?",
|
48 |
+
"Can I see samples of Rookus' designs?",
|
49 |
+
"How can I join the waitlist for Rookus?",
|
50 |
+
"How does Rookus ensure the quality of its AI-generated designs?",
|
51 |
+
"Is there a custom design option available at Rookus?",
|
52 |
+
"How long does it take to receive a product from Rookus?"
|
53 |
+
],
|
54 |
+
"answers": [
|
55 |
+
"Rookus is a startup that leverages AI to create unique designs for various products such as clothes, posters, and different arts and crafts.",
|
56 |
+
"Rookus uses advanced AI algorithms to generate innovative and aesthetically pleasing designs. These AI models are trained on vast datasets of art and design to produce high-quality mockups.",
|
57 |
+
"Rookus offers a variety of products, including clothing, posters, and a range of arts and crafts items, all featuring AI-generated designs.",
|
58 |
+
"Yes, Rookus provides samples of its designs on its website. You can view a gallery of products showcasing the AI-generated artwork.",
|
59 |
+
"To join the waitlist for Rookus, visit our website and sign up with your email. You'll receive updates on our launch and exclusive early access opportunities.",
|
60 |
+
"Rookus ensures the quality of its AI-generated designs through rigorous testing and refinement. Each design goes through multiple review stages to ensure it meets our high standards.",
|
61 |
+
"Yes, Rookus offers custom design options. You can submit your preferences, and our AI will generate a design tailored to your specifications.",
|
62 |
+
"The delivery time for products from Rookus varies based on the product type and location. Typically, it takes 2-4 weeks for production and delivery."
|
63 |
+
],
|
64 |
+
"default_answer": "I'm sorry, I cannot answer this right now. Your question has been saved, and we will get back to you with a response soon."
|
65 |
+
}
|
66 |
+
|
67 |
+
# Initialize GPT-2 model and tokenizer
|
68 |
+
gpt2_tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
|
69 |
+
gpt2_model = GPT2LMHeadModel.from_pretrained('gpt2')
|
70 |
+
|
71 |
+
# Ensure the Excel file is created with necessary structure
|
72 |
+
excel_file = 'data.xlsx'
|
73 |
+
if not os.path.isfile(excel_file):
|
74 |
+
df = pd.DataFrame(columns=['question'])
|
75 |
+
df.to_excel(excel_file, index=False)
|
76 |
+
|
77 |
+
def chatbot(user_query):
|
78 |
+
closest_question, similarity = get_closest_question(user_query, data_dict['questions'], threshold=0.95)
|
79 |
+
if closest_question and similarity >= 0.95:
|
80 |
+
answer_index = data_dict['questions'].index(closest_question)
|
81 |
+
answer = data_dict['answers'][answer_index]
|
82 |
+
else:
|
83 |
+
new_data = pd.DataFrame({'question': [user_query]})
|
84 |
+
df = pd.read_excel(excel_file)
|
85 |
+
df = pd.concat([df, new_data], ignore_index=True)
|
86 |
+
with pd.ExcelWriter(excel_file, engine='openpyxl', mode='w') as writer:
|
87 |
+
df.to_excel(writer, index=False)
|
88 |
+
answer = data_dict['default_answer']
|
89 |
+
|
90 |
+
return answer
|
91 |
+
|
92 |
+
# Gradio Interface
|
93 |
+
iface = gr.Interface(fn=chatbot, inputs="text", outputs="text")
|
94 |
+
|
95 |
+
# FastAPI endpoint
|
96 |
+
class Query(BaseModel):
|
97 |
+
user_query: str
|
98 |
+
|
99 |
+
@app.post("/api/chatbot")
|
100 |
+
async def get_answer(query: Query):
|
101 |
+
user_query = query.user_query
|
102 |
+
return {"answer": chatbot(user_query)}
|
103 |
+
|
104 |
+
# Run the app with Uvicorn
|
105 |
+
if __name__ == "__main__":
|
106 |
+
iface.launch(share=True)
|
107 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|