shahiil commited on
Commit
cf6f59d
·
verified ·
1 Parent(s): 0458ecb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
2
+ import gradio as gr
3
+
4
+ # Load pre-trained Hugging Face model
5
+ model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" # Replace with your model
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
7
+ model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=True)
8
+
9
+ # Initialize text generation pipeline
10
+ text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
11
+
12
+ def laptop_recommendation(user_input, task):
13
+ """
14
+ Handles laptop recommendation tasks based on user preferences.
15
+ """
16
+ if not user_input.strip():
17
+ return "Please provide some input."
18
+
19
+ if task == "Recommendation":
20
+ prompt = f"Recommend a laptop based on the following preferences:\n{user_input}\nRecommended Laptop:"
21
+ elif task == "Compare":
22
+ prompt = f"Compare two laptops based on the following specifications:\n{user_input}\nComparison:"
23
+ elif task == "Budget Recommendation":
24
+ prompt = f"Recommend the best laptop for the following budget:\n{user_input}\nRecommended Laptop for Budget:"
25
+ else:
26
+ return "Invalid task selected."
27
+
28
+ try:
29
+ response = text_generator(
30
+ prompt,
31
+ max_length=96,
32
+ num_return_sequences=1,
33
+ pad_token_id=tokenizer.eos_token_id,
34
+ temperature=0.7,
35
+ top_p=0.9
36
+ )[0]["generated_text"]
37
+ return response[len(prompt):].strip()
38
+ except Exception as e:
39
+ return f"An error occurred during text generation: {str(e)}"
40
+
41
+ def gradio_interface(user_input, task):
42
+ """Gradio interface function."""
43
+ return laptop_recommendation(user_input, task)
44
+
45
+ with gr.Blocks() as laptop_recommendation_ui:
46
+ gr.Markdown("# Laptop Recommendation Chatbot")
47
+ gr.Markdown(
48
+ "This chatbot helps with recommending laptops based on preferences, comparing laptops, and suggesting options based on budget."
49
+ )
50
+
51
+ user_input = gr.Textbox(lines=5, placeholder="Enter your laptop preferences here...", label="Your Input")
52
+ task = gr.Radio(["Recommendation", "Compare", "Budget Recommendation"], label="Select Task")
53
+ output = gr.Textbox(lines=10, label="Chatbot Response")
54
+
55
+ submit_button = gr.Button("Submit")
56
+ clear_button = gr.Button("Clear")
57
+
58
+ submit_button.click(gradio_interface, inputs=[user_input, task], outputs=output)
59
+ clear_button.click(lambda: ("", ""), None, [user_input, output])
60
+
61
+ laptop_recommendation_ui.launch()