Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,50 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
|
|
3 |
|
4 |
-
# πΉ Load
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
|
|
|
|
|
8 |
def extract_interests(text):
|
9 |
-
prompt = f"Extract 3-5
|
10 |
-
|
11 |
-
response = pipe(prompt, max_length=50, num_return_sequences=1)
|
12 |
interests = response[0]["generated_text"].replace(prompt, "").strip()
|
13 |
-
|
14 |
return interests.split(", ")
|
15 |
|
16 |
# π Web search for gift suggestions
|
17 |
def search_gifts(interests):
|
18 |
query = "+".join(interests)
|
19 |
-
amazon_url = f"https://www.amazon.in/s?k={query}"
|
20 |
-
flipkart_url = f"https://www.flipkart.com/search?q={query}"
|
21 |
-
igp_url = f"https://www.igp.com/search?q={query}"
|
22 |
-
indiamart_url = f"https://dir.indiamart.com/search.mp?ss={query}"
|
23 |
-
|
24 |
return {
|
25 |
-
"Amazon":
|
26 |
-
"Flipkart":
|
27 |
-
"IGP":
|
28 |
-
"IndiaMart":
|
29 |
}
|
30 |
|
31 |
# π― Main function for gift recommendation
|
32 |
def recommend_gifts(text):
|
33 |
if not text:
|
34 |
return "Please enter a description."
|
35 |
-
|
36 |
interests = extract_interests(text)
|
37 |
links = search_gifts(interests)
|
38 |
|
39 |
-
return {
|
40 |
-
"Predicted Interests": interests,
|
41 |
-
"Gift Suggestions": links
|
42 |
-
}
|
43 |
|
44 |
-
# π¨ Gradio UI
|
45 |
demo = gr.Interface(
|
46 |
fn=recommend_gifts,
|
47 |
inputs="text",
|
48 |
outputs="json",
|
49 |
-
title="π AI Gift Recommender",
|
50 |
-
description="Enter details about the person
|
51 |
)
|
52 |
|
53 |
# π Launch Gradio App
|
54 |
if __name__ == "__main__":
|
55 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
3 |
+
import torch
|
4 |
|
5 |
+
# πΉ Load Llama 3 - 7B model
|
6 |
+
model_name = "meta-llama/Meta-Llama-3-7B-Instruct"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
9 |
|
10 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=100)
|
11 |
+
|
12 |
+
# π― Extract interests from user input
|
13 |
def extract_interests(text):
|
14 |
+
prompt = f"Extract 3-5 keywords from this: '{text}' (focus on hobbies & products)."
|
15 |
+
response = pipe(prompt)
|
|
|
16 |
interests = response[0]["generated_text"].replace(prompt, "").strip()
|
|
|
17 |
return interests.split(", ")
|
18 |
|
19 |
# π Web search for gift suggestions
|
20 |
def search_gifts(interests):
|
21 |
query = "+".join(interests)
|
|
|
|
|
|
|
|
|
|
|
22 |
return {
|
23 |
+
"Amazon": f"https://www.amazon.in/s?k={query}",
|
24 |
+
"Flipkart": f"https://www.flipkart.com/search?q={query}",
|
25 |
+
"IGP": f"https://www.igp.com/search?q={query}",
|
26 |
+
"IndiaMart": f"https://dir.indiamart.com/search.mp?ss={query}",
|
27 |
}
|
28 |
|
29 |
# π― Main function for gift recommendation
|
30 |
def recommend_gifts(text):
|
31 |
if not text:
|
32 |
return "Please enter a description."
|
33 |
+
|
34 |
interests = extract_interests(text)
|
35 |
links = search_gifts(interests)
|
36 |
|
37 |
+
return {"Predicted Interests": interests, "Gift Suggestions": links}
|
|
|
|
|
|
|
38 |
|
39 |
+
# π¨ Gradio UI
|
40 |
demo = gr.Interface(
|
41 |
fn=recommend_gifts,
|
42 |
inputs="text",
|
43 |
outputs="json",
|
44 |
+
title="π AI Gift Recommender (Llama 3 - 7B)",
|
45 |
+
description="Enter details about the person and get personalized gift suggestions with shopping links!",
|
46 |
)
|
47 |
|
48 |
# π Launch Gradio App
|
49 |
if __name__ == "__main__":
|
50 |
+
demo.launch()
|