Afeefa123 commited on
Commit
90aa2a3
·
verified ·
1 Parent(s): 0eba4fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -66
app.py CHANGED
@@ -1,67 +1,36 @@
1
- import os
2
- import numpy as np
3
- import pandas as pd
4
- import faiss
5
  from groq import Groq
6
- from huggingface_hub import InferenceClient # If you need Hugging Face support
7
- import radio # Assuming you meant `radio` as a placeholder for UI or any framework
8
-
9
- # Set Groq API Key
10
- os.environ["GROQ_API_KEY"] = "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"
11
- client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
12
-
13
- # Function to query Groq API
14
- def query_groq(prompt, model="llama3-8b-8192"):
15
- chat_completion = client.chat.completions.create(
16
- messages=[
17
- {"role": "user", "content": prompt}
18
- ],
19
- model=model,
20
- )
21
- return chat_completion.choices[0].message.content
22
-
23
- # Simulated Traffic Dataset
24
- data = {
25
- "Traffic_Density": [30, 60, 90, 70], # Vehicles/km
26
- "Signal_Wait_Time": [2, 5, 10, 7], # in minutes
27
- "Weather_Condition": ["Clear", "Rainy", "Foggy", "Clear"], # categorical
28
- "Time_of_Day": ["Morning", "Evening", "Night", "Afternoon"], # categorical
29
- "Avg_Speed": [50, 30, 20, 40], # km/h
30
- }
31
- df = pd.DataFrame(data)
32
-
33
- # Encode categorical columns
34
- df_encoded = pd.get_dummies(df, columns=["Weather_Condition", "Time_of_Day"])
35
-
36
- # FAISS index setup
37
- def build_faiss_index(df_vectorized):
38
- data_values = df_vectorized.values.astype('float32')
39
- index = faiss.IndexFlatL2(data_values.shape[1])
40
- index.add(data_values)
41
- return index
42
-
43
- index = build_faiss_index(df_encoded.drop(columns=["Avg_Speed"]))
44
- query_vector = np.array([[60, 5, 1, 0, 0, 0, 0, 1, 0, 0]]).astype('float32') # Sample
45
-
46
- # Nearest Neighbor Search
47
- D, I = index.search(query_vector, k=2)
48
- print("Distances:", D)
49
- print("Indices:", I)
50
-
51
- # Generate Recommendations
52
- def generate_traffic_insights(input_vector):
53
- D, I = index.search(np.array([input_vector]).astype('float32'), k=3)
54
- similar_conditions = df.iloc[I[0]]
55
- print("Similar Conditions:\n", similar_conditions)
56
-
57
- prompt = (
58
- f"Given traffic data: {similar_conditions.to_dict(orient='records')}, "
59
- "suggest strategies to optimize traffic flow and reduce signal wait times."
60
- )
61
- response = query_groq(prompt)
62
- return response
63
-
64
- # Use Case: Predict & Recommend
65
- input_vector = [60, 5, 1, 0, 0, 0, 0, 1, 0, 0] # Example input
66
- insights = generate_traffic_insights(input_vector)
67
- print("Traffic Optimization Insights:\n", insights)
 
 
 
 
 
1
  from groq import Groq
2
+ import gradio as gr
3
+
4
+ # Groq API key (hardcoded as requested)
5
+ GROQ_API_KEY = "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"
6
+
7
+ # Initialize Groq client
8
+ client = Groq(api_key=GROQ_API_KEY)
9
+
10
+ # Function to get traffic recommendation
11
+ def get_traffic_recommendation(input_situation):
12
+ try:
13
+ chat_completion = client.chat.completions.create(
14
+ messages=[
15
+ {
16
+ "role": "user",
17
+ "content": f"Suggest a solution to optimize traffic flow in the following situation:\n\n{input_situation}"
18
+ }
19
+ ],
20
+ model="llama-3-70b-8192"
21
+ )
22
+ return chat_completion.choices[0].message.content
23
+ except Exception as e:
24
+ return f"Error: {e}"
25
+
26
+ # Gradio UI
27
+ demo = gr.Interface(
28
+ fn=get_traffic_recommendation,
29
+ inputs=gr.Textbox(lines=5, placeholder="Describe the traffic scenario..."),
30
+ outputs="text",
31
+ title="Traffic Flow Optimizer",
32
+ description="Enter a traffic scenario to get suggestions for improving traffic flow using the LLaMA-3 model powered by Groq."
33
+ )
34
+
35
+ if __name__ == "__main__":
36
+ demo.launch()