huzaifa456 commited on
Commit
59a34af
·
verified ·
1 Parent(s): 65142a5

initial commit

Browse files

hi everyone it is my very first hugging face repo.
i hope it will be fine when you will use
by by.

Files changed (2) hide show
  1. app.py +201 -0
  2. requirements.txt +16 -0
app.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ import matplotlib.pyplot as plt
4
+
5
+ st.tittle("Chatwork")
6
+
7
+ from langchain.tools import WikipediaQueryRun
8
+ from langchain_community.utilities import WikipediaAPIWrapper
9
+
10
+ wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
11
+
12
+ from langchain.agents import Tool
13
+
14
+ tools = [
15
+ Tool(
16
+ name = "Wikipedia",
17
+ func=wikipedia.run,
18
+ description="Usefull for answering every Wikipedia"
19
+ )
20
+ ]
21
+
22
+ from langchain_google_genai import ChatGoogleGenerativeAI
23
+ llm = ChatGoogleGenerativeAI(model="gemini-1.5-pro", temperature=0.8, google_api_key="AIzaSyBOARVkuQ1nbaQCnKrRHqRUDqklP9JfdKY")
24
+
25
+ from langchain.agents import initialize_agent, AgentType
26
+
27
+ agent = initialize_agent(tools , llm, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
28
+
29
+ agent.invoke({"input": "tell me the most funny summary ever about poets"})
30
+ import requests
31
+ from langchain_core.tools import tool
32
+
33
+ @tool
34
+ def get_joke(joke_type: str="Any") -> str:
35
+ """Generate a joke of the specified type."""
36
+ url = f"https://v2.jokeapi.dev/joke/{joke_type}"
37
+ response = requests.get(url)
38
+ joke_data = response.json()
39
+
40
+ if joke_data.get("error"):
41
+ return "No jokes found for the specified type."
42
+
43
+ if joke_data["type"] == "single":
44
+ return joke_data["joke"]
45
+ else:
46
+ return f"{joke_data['delivery']}"
47
+
48
+
49
+
50
+ print(get_joke("Any"))
51
+
52
+ from langchain_groq import ChatGroq
53
+
54
+ #llm init
55
+ llm = ChatGroq(
56
+ api_key="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",
57
+ verbose=True,
58
+ model="llmama3-8b-8192"
59
+ )
60
+
61
+ import os
62
+ from textwrap import dedent
63
+ # Ensure correct module name and path
64
+ from crew_ai.tools import Agent
65
+ from crewai_tools import ScrapeWebsiteTool, SerperDevTool
66
+
67
+ os.environ["SERPER_API_KEY"] = "294d3cb863f951cea074386f5340e5df46955d47"
68
+
69
+ def web_researcher_agent(topic):
70
+ return Agent(
71
+ role="Expert on every question. Answer/web researcher and a voice agent.",
72
+ goal=dedent(f"""your goal is to search the web or answer the question of the user search the web or scrape. and search the answer from wikipedia and reply also in text and in voice like google voice agent or serie. use the tool provided to search/and answer any random question or make the user choice graph and creating images.
73
+ Topic: {topic}"""),
74
+ tools=[ScrapeWebsiteTool(), SerperDevTool()],
75
+ backstory=dedent("""You are a professional for searching for specific topics on the web or searching any prompt of user at wikipedia search the best information and creating images."""),
76
+ verbose=True,
77
+ allow_delegation=False,
78
+ llm=llm
79
+ )
80
+ from diffusers import StableDiffusionPipeline
81
+ import torch
82
+
83
+ model_id = "runwayml/stable-diffusion-v1-5"
84
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
85
+ pipe = pipe.to("cuda")
86
+
87
+ prompt = ""
88
+ image = pipe(prompt).images[0]
89
+
90
+ image.save("image.png")
91
+
92
+ from os import system
93
+ from langchain_core.tools import tool
94
+
95
+ @tool
96
+ def create_image(prompt: str) -> str:
97
+ """Create stunning AI Generated Images"""
98
+ image = pipe(prompt).images[0]
99
+ image.save("image.png")
100
+ return "Image saved on user device."
101
+
102
+ print(create_image(""))
103
+
104
+ tools = [
105
+ create_image
106
+ ]
107
+
108
+ from langchain import hub
109
+
110
+ prompt = hub.pull("hwchase17/openai-tools-agent")
111
+ print(prompt.messages[0].prompt.template)
112
+
113
+ prompt.messages[0].prompt.template = """
114
+
115
+ You are an expert in generating detailed prompts for AI generated images.
116
+
117
+ The user will provide you a basic prompt and your goal is to convert it into a detailed but SHORT prompt for an AI generated image.
118
+
119
+ After creating a prompt use the create_image tool to create the image with the prompt.
120
+
121
+ Respond with just "Image generated" when tool call is succesfull.
122
+
123
+ Here is an example:
124
+
125
+ User: "I want you to create a prompt for a girl looking over a battle with a sword in her hand in a fantasy book"
126
+
127
+ AI: "Create an illustration of a young girl standing on a cliff, overlooking a vast battlefield with a sword in her hand, ready for battle. The scene should be set in a high-fantasy world, with a medieval-inspired aesthetic. The girl should be wearing armor that is practical and functional for combat, but also displays her personal flair and style. The landscape should be mountainous and rugged, with dramatic clouds in the sky and hints of magic or otherworldly elements. The overall tone should be serious and intense, with a sense of impending danger and conflict. Use a color palette that is bold and striking, emphasizing the contrast between light and shadow. The art style should be highly detailed and polished, with influences from classic fantasy illustrators such as Frank Frazetta and Boris Vallejo."
128
+ AI: invoke create_image tool with the prompt.
129
+
130
+ """
131
+
132
+ from langchain_groq import ChatGroq
133
+
134
+ #llm init
135
+ llm = ChatGroq(
136
+ api_key="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",
137
+ verbose=True,
138
+ model="llmama3-8b-8192"
139
+ )
140
+
141
+ from langchain.agents import AgentExecutor,create_tool_calling_agent
142
+
143
+ agent = create_tool_calling_agent(llm, tools, prompt)
144
+
145
+ agent_executer = AgentExecutor(agent=agent, tools=tools, verbose=True)
146
+
147
+ agent_executer.invoke({"input":""})
148
+
149
+ import speech_recognition as sr
150
+ import pyttsx3
151
+ from langchain import Agent, Memory
152
+ from langchain.llms import Groq
153
+
154
+ recognizer = sr.Recognizer()
155
+ tts_engine = pyttsx3.init()
156
+
157
+ llm = Groq(api_key='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')
158
+
159
+ memory = Memory()
160
+
161
+ agent = Agent(llm, memory=memory)
162
+
163
+ def recognize_speech():
164
+ with sr.Microphone() as source:
165
+ print("Listening...")
166
+ audio = recognizer.listen(source)
167
+ try:
168
+ text = recognizer.recognize_google(audio)
169
+ print(f": {text}")
170
+ return text
171
+ except sr.UnknownValueError:
172
+ print("")
173
+ return None
174
+ except sr.RequestError as e:
175
+ print(f"; {e}")
176
+ return None
177
+
178
+ def speak_text(text):
179
+ tts_engine.say(text)
180
+ tts_engine.runAndWait()
181
+
182
+ def main():
183
+ print("Voice Agent is running. Say 'stop' to end the session.")
184
+ while True:
185
+ command = recognize_speech()
186
+ if command:
187
+ if 'stop' in command.lower():
188
+ print("Stopping the voice agent.")
189
+ break
190
+
191
+ # Process the command and store it in memory
192
+ response = agent.run(command)
193
+
194
+ # Store the interaction in memory
195
+ memory.store(command, response)
196
+
197
+ print(f"Agent: {response}")
198
+ speak_text(response)
199
+
200
+ if __name__ == "__main__":
201
+ main()
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ crewai_tools==0.8.3
2
+ diffusers==0.30.0
3
+ langchain==0.2.12
4
+ langchain_community==0.2.11
5
+ langchain_core==0.2.29
6
+ langchain_google_genai==1.0.8
7
+ langchain_groq==0.1.9
8
+ matplotlib==3.7.1
9
+ pyttsx3==2.90
10
+ Requests==2.32.
11
+ SpeechRecognition==3.10.4
12
+ streamlit==1.37.1
13
+ torch==2.3.1+cu121
14
+ wikipedia
15
+ streamlit
16
+ langchain_groq