rishabh5752 commited on
Commit
ffc675f
·
1 Parent(s): 9f1d5f0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cohere
3
+ import numpy as np
4
+ import warnings
5
+ from annoy import AnnoyIndex
6
+
7
+ # Cohere API Key
8
+ cohere_api_key = "4GMQpN0nV9chIaV3DTQNmVYiskbTZdDBPrvw7pPs"
9
+ co = cohere.Client(cohere_api_key)
10
+
11
+ # Load the pre-built Annoy index
12
+ search_index = AnnoyIndex(512, 'angular')
13
+ search_index.load('test.ann')
14
+
15
+ # Define a function to search for information
16
+ def search_text(query):
17
+ query_embed = co.embed(texts=[query]).embeddings
18
+ similar_item_ids = search_index.get_nns_by_vector(query_embed[0], 10, include_distances=True)
19
+ search_results = texts[similar_item_ids[0]]
20
+ return search_results
21
+
22
+ # Define a function to ask questions
23
+ def ask_llm(question, num_generations=1):
24
+ results = search_text(question)
25
+ context = results[0]
26
+ prompt = f"""
27
+ More information about Australian beaches at australia.com:
28
+ {context}
29
+ Question: {question}
30
+ Extract the answer of the question from the text provided.
31
+ If the text doesn't contain the answer,
32
+ reply that the answer is not available."""
33
+ prediction = co.generate(
34
+ prompt=prompt,
35
+ max_tokens=70,
36
+ model="command-nightly",
37
+ temperature=0.5,
38
+ num_generations=num_generations
39
+ )
40
+ return prediction.generations
41
+
42
+ # Define your text data
43
+ question = "Which Sydney beach should I visit?"
44
+ text = """
45
+ Sydney is world famous for beautiful beaches. These beaches offer different vibes and attractions, from bustling crowds and great surfing conditions to more tranquil and family-friendly environments.
46
+ Bondi Beach: Bondi is perhaps the most famous beach in Sydney, if not Australia. It's known for its golden sands, vibrant atmosphere, and excellent surfing conditions. The Bondi to Coogee coastal walk offers stunning views of the coastline.
47
+ Manly Beach: Easily accessible by a scenic ferry ride from Circular Quay, Manly Beach is known for its laid-back atmosphere and family-friendly environment. The beach is great for swimming, surfing, and various water sports.
48
+ Cronulla Beach: Located in the Sutherland Shire, Cronulla offers a more relaxed atmosphere compared to some of the busier city beaches. It's a great spot for swimming, picnicking, and enjoying a range of seaside cafes and restaurants.
49
+ Bronte Beach: Situated between Bondi and Coogee, Bronte Beach is popular among both locals and visitors. It's a smaller, quieter beach with a beautiful park area and a natural rock pool that's ideal for swimming.
50
+ Tamarama Beach: Also known as "Glamarama" due to its popularity among the fashion-conscious crowd, Tamarama Beach is a smaller and more secluded option. It's surrounded by rocky cliffs and offers strong surf, making it a favorite among experienced surfers.
51
+ """
52
+ texts = text.split('\n\n')
53
+ texts = np.array([t.strip(' \n') for t in texts if t])
54
+
55
+ # Create Gradio interface
56
+ iface = gr.Interface(
57
+ fn=ask_llm,
58
+ inputs=gr.inputs.Textbox(text_type="text", label="Ask a question about Sydney beaches:"),
59
+ outputs="text",
60
+ title="Sydney Beaches Information",
61
+ live=True,
62
+ capture_session=True,
63
+ )
64
+
65
+ # Launch the Gradio interface
66
+ iface.launch(share=True)