Update app.py
Browse files
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 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|