Afaq5486/Civil_Engineering_AI_Chat
0
1import gradio as gr2import requests3 4# ๐ Hardcode your Groq API key here5GROQ_API_KEY = "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"6 7# System prompt8system_prompt = {9 "role": "system",10 "content": (11 "You are a highly knowledgeable and helpful AI Civil Engineering Assistant. "12 "You are trained to answer any question related to civil engineering, including structural analysis, "13 "concrete and steel design, geotechnical engineering, construction materials, surveying, transportation, "14 "and environmental engineering. Always provide accurate, step-by-step explanations. Clearly formulate the problem, "15 "define given data, and explain the reasoning behind calculations or decisions using proper terminology. "16 "If a question is ambiguous, ask clarifying questions."17 )18}19 20# Chat function21def chat(user_message, history):22 # Build message history23 messages = [system_prompt]24 for user, bot in history:25 messages.append({"role": "user", "content": user})26 messages.append({"role": "assistant", "content": bot})27 messages.append({"role": "user", "content": user_message})28 29 try:30 response = requests.post(31 "https://api.groq.com/openai/v1/chat/completions",32 headers={33 "Authorization": f"Bearer {GROQ_API_KEY}",34 "Content-Type": "application/json",35 },36 json={37 "model": "mixtral-8x7b-32768", # or llama3-70b-819238 "messages": messages,39 "temperature": 0.7,40 },41 timeout=30,42 )43 response.raise_for_status()44 reply = response.json()["choices"][0]["message"]["content"]45 history.append((user_message, reply))46 return history, ""47 48 except Exception as e:49 error_msg = f"โ Error: {e}"50 history.append((user_message, error_msg))51 return history, ""52 53# Gradio UI54with gr.Blocks() as demo:55 gr.Markdown("## ๐๏ธ Civil Engineering AI Chatbot")56 gr.Markdown("Ask any question about civil engineering and get a detailed answer.")57 58 chatbot = gr.Chatbot()59 message = gr.Textbox(label="Ask a question")60 state = gr.State([])61 62 message.submit(chat, inputs=[message, state], outputs=[chatbot, message, state])63 demo.launch()64 